Any object should posses
- Identity
- State
- Behavior
- Declare a reference variable
- Construct object with "new" keyword. new is used for dynamic memory allocation.
- Assign the reference variable to new keyword. and construct the object.
- class A{
- int a,b;
- static int x,y;
- A(){
- }
- void xy(){
- }
- }
- class B{
- void main(){
- A obj = new A();
- obj.xy();
- }
- }
the new A() (line 11) is called as object which refers to class A. the obj (line 11) is called as reference variable of class A. when obj is created then the space for a,b (line 2) gets created in heap, for remaining variables and methods its created in class memory. as the same time the constructor of class A gets called. If its not present the java creates a empty constructor.
I hope the concept of object is understood if any queries comment below and stay tuned for more blogs.....
Quiz to solve : Find objects and class in this picture ? (post your answers as your comments)
Follow me
Comments
Post a Comment