Introduction to Java Classes and Objects

      No Comments on Introduction to Java Classes and Objects

Introduction to Java

Introduction to Java

What kind of variables a class can consist of?

A class consists of Local variable, instance variables and class variables.

in Introduction to Java What do you mean by Local Variable?

Variables defined inside methods, constructors or blocks are called local variables.  The variable will be declared and initialized within the method and it will be destroyed when the method has completed.

What is an Instance Variable?

Instance variables are variables within a class but outside any method. These variables are instantiated when the class is loaded.

What is a Class Variable?

These are variables declared with in a class, outside any method, with the static keyword.

What do you mean by Constructor?

Constructor gets invoked when a new object is created. Every class has a constructor. If we do not explicitly write a constructor for a class the java compiler builds a default constructor for that class.

What is a static variable?

Class variables also known as static variables are declared with the static keyword in a class, but outside a method, constructor or a block.

What is finalize() method?

It is possible to define a method that will be called just before an object’s final destruction by the garbage collector. This method is called finalize( ), and it can be used to ensure that an object terminates cleanly.

Explain garbage collection in Java?

It uses garbage collection to free the memory. By cleaning those objects that is no longer reference by any of the program.

Define immutable object?

An immutable object can’t be changed once it is created.

Explain the usage of this with constructors?

It is used with variables or methods and used to call constructer of same class.

What is the purpose of default constructor?

The java compiler creates a default constructor only if there is no constructor in the class.

Can a constructor be made final?

No, this is not possible.

What is static block?

It is used to initialize the static data member, It is excuted before main method at the time of classloading.

What is the difference between static and non-static variables?

A static variable is associated with the class as a whole rather than with specific instances of a class. Non-static variables take on unique values with each object instance.

What’s the difference between constructors and other methods?

Constructors must have the same name as the class and can not return a value. They are only called once while regular methods could be called many times.

Can you call one constructor from another if a class has multiple constructors?

Yes, use this() syntax.

Leave a Reply