Java Class and Object

 Q. What are Classes in Java? 

In Java, Classes are the collection of objects sharing similar characteristics and attributes. 
Classes represent the blueprint or template from which objects are created. 
Classes are not real-world entities but help us to create objects which are real-world entities. 

Q. What is an object?
The object is a real-life entity that has certain properties and methods associated with it. 
The object is also defined as the instance of a class. 
An object can be declared using a new keyword.

Q. What are the different ways to create objects in Java?
Methods to create objects in Java are mentioned below:
Using new keyword
Using new instance
Using clone() method
Using deserialization
Using the newInstance() method of the Constructor class

Q. What will be the initial value of an object reference which is defined as an instance variable?
The initial value of an object reference which is defined as an instance variable is a NULL value.

Q. What is the constructor?
Constructor is a special method that is used to initialize objects. 
Constructor is called when a object is created. 
The name of constructor is same as of the class.
If you don't provide a constructor in a class in Java, the compiler automatically generates a default constructor with no arguments and no operation which is a default constructor.
There are two types of constructors in Java as mentioned below:
Default Constructor
Parameterized Constructor

Q. What is a Class Variable?
In Java, a class variable (also known as a static variable) is a variable that is declared within a class but outside of any method, constructor, or block. 
Class variables are declared with the static keyword, and they are shared by all instances (objects) of the class as well as by the class itself. 
No matter how many objects are derived from a class, each class variable would only exist once.

Q. What is a Instance variable?
A class variable without the static modifier is called an instance variable. 
It is unique to each object (instance) of the class and is not shared between instances.
Declared outside the method, directly invoked by the method.
Has a default value.
It can be used throughout the class.

Q. What is a static variable?
The static keyword is used to share the same variable or method of a given class. Static variables are the variables that once declared then a single copy of the variable is created and shared among all objects at the class level.

Q. What is a Local variable?
Declared within the method.   
No default value.
The scope is limited to the method.

Q. Explain public static void main(String args[]) in Java.
public: the public is the access modifier responsible for mentioning who can access the element or the method and what is the limit.  It is responsible for making the main function globally available. It is made public so that JVM can invoke it from outside the class as it is not present in the current class.
static: static is a keyword used so that we can use the element without initiating the class so to avoid the unnecessary allocation of the memory. 
void: void is a keyword and is used to specify that a method doesn’t return anything. As the main function doesn't return anything we use void.
main: main represents that the function declared is the main function. It helps JVM to identify that the declared function is the main function.
String args[]: It stores Java command-line arguments and is an array of type java.lang.String class.

Q. What will happen if we don't declare the main as static?
We can declare the main method without using static and without getting any errors. But, the main method will not be treated as the entry point to the application or the program.

Q. What are Packages in Java?
Packages in Java can be defined as the grouping of related types of classes, interfaces, etc providing access to protection and namespace management.
Packages are used in Java in order to prevent naming conflicts, control access, and make searching/locating and usage of classes, interfaces, etc easier.
There are various advantages of defining packages in Java.
1. Packages avoid name clashes.
2. The Package provides easier access control.
3. We can also have the hidden classes that are not visible outside and are used by the package.
4. It is easier to locate the related classes.
There are two types of packages in Java
1. User-defined packages
2. Build In packages


Popular posts from this blog

IT Software Services

Agile Ceremonies

Learn Programming