Objects And Class Designing In Java
We know that classes are the collection of objects. So, in this page we will learn about creating objects and classes. So lets start with the objects:
Object in Java
An entity that has behaviour and properties is known as Object.
For Example: Car: it belongs to a company and has a name and colour, So these are its properties and it is used for transportation is its behaviour. Objects are instance of a class.
Class In Java
A class in Java is collection of objects. We can say that it is a blueprint from which objects are created.
A class may contain these inside it:
1. Data members
2. Methods
3. Constructors
4. Inner Classes
5. Blocks (static, init etc)
So how we create classes and objects??
Let us try to understand classes and object with the help of an example:
Java
class Demo //we define class as >>>classClassname
{
int salary; //a data member (by default all non static
//data members are instance)
String name; //another data member
Demo() //constructor defined
{
name = "Arvind"; //values initialized in the constructors
salary = 1000; //values initialized
}
public static void main(String[] args) //main method
{
Demo d = new Demo(); //creating the object of Demo class
System.out.println(d.name); //printing the values of data members
System.out.println(d.salary); //printing the values of data members
}
}
In the above program, we defined a simple “class” with class name “Demo”. we defined two data members in it. one of int type and other data member is of String type.
In the main method we created an Object of Demo class by using a “new” keyword.
--------------------------------------------------------------------------------------------------------------------------
Question: So how does the object created here?
Demo d = new Demo();
> Whenever a “new” keyword is used in the program a memory area is reserved in the RAM by the JVM. And to identify for which class it is created we defined the constructor “Demo()” of Demo() class after the “new” keyword. After the object created in the memory, the data members gets the memory in the Object and their values are initialized via constructor which is added by the JVM implicitly when no constructor is defined in the program.
Question: What is Constructor now?
> Constructor is a special type of method that is used to initialize the object. And its name is similar to the name of class.
Question: what is instance or instance variable?
> Instance variable are non static variables that are defined inside the class but outside the method body(class level variable). If data members of a class is not static then by default they are instance.
> They do not get memory at compile time. They gets memory when the Object is created in the memory. And Object is created at runtime.
> To access instance variables in staticmethods,we need to create the class Object first.
Question: What is Anonymous Object?
Anonymous means nameless and a person without any identity. Similarly in Java, an Object who have no reference is known as anonymous Object. This is very useful if we do not have to reuse our class object. If we do not want to reuse our Object then make them anonymous.
Question: How to make our Object Anonymous?
Consider this example:
In this example we created a anonymous Object of our Demo class to access instance variable.
Java
class Demo //we define class as >>>classClassname
{
String name; //instance data member
Demo()
{
name = "Arvind";
}
public static void main(String[] args) //main method
{
System.out.println(new Demo().name); //anonymous Object created.
//we did not save Object's reference to any variable.
//we cannot use anonymous Object later.
}
}
--------------------------------------------------------------------------------------------------------------------------
Points To Remember
· The portion of a class which is getting a memory at class loading time, that can get the memory only once into the life cycle of a class and this process is known as static memory allocation.
· The portion of a class which is getting a memory at run time, that can get the memory again and again into the life cycle of a class and this process is known as dynamic memory allocation.
· All the function of a class are getting memory at class loading time in a area called method area which is the part of a class area.
· In Java object is nothing, it is just a buffer/memory area in a heap in which all the instance data member of a class are getting memory.
· Objects are run time entity in Java. Means they always get memory at runtime.
· In Java only Objects are getting memory at runtime.
· “new” operator is only used for allocating a memory to an object.
No comments:
Post a Comment