wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes difference between object and instance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "difference between object and instance" Watch "difference between object and instance" New topic
Author

difference between object and instance

Phillipe Rodrigues
Ranch Hand

Joined: Oct 30, 2007
Posts: 165
I'm getting jumbled up with the following terms:
1. object of the class
2. instance of the class

what is the difference between the two terms object and instance.
The above is possing problems for me to proceed.



As I'm not clear with the above I'm not able to understand the below :

Static variables (also called class variables) only exist in the class they are defined in. They are not instantiated when an instance of the class is created. In other words, the values of these variables are not a part of the state of any object.


Request your help. The above is possing problems for me to proceed.


Thanks,
Chetan Parekh
Ranch Hand

Joined: Sep 16, 2004
Posts: 3636
My understanding is that both are the same.


My blood is tested +ve for Java.
Anubhav Anand
Ranch Hand

Joined: May 18, 2007
Posts: 341

Not at all an advanced question.

Well, instance and object are different terminologies for the same concept. Just, the usage differs at times.

Static variables (also called class variables) only exist in the class they are defined in. They are not instantiated when an instance of the class is created. In other words, the values of these variables are not a part of the state of any object.


Well, the above qoute states that Static variables are not specific to an instance of a class. A static variable is instantiated just once and all the further objects that are created thereafter share the same copy of the static variable. So, this in turn means when an object is serialized then as the object will not have its own copy of the static variable so, the static variable will not be part of the state of the object.
[ March 05, 2008: Message edited by: Anubhav Anand ]
Phillipe Rodrigues
Ranch Hand

Joined: Oct 30, 2007
Posts: 165
with the below said about Static members,I tried an example:
Static members belong to the class in which they are declared, and are not part of any instance of the class. Static variables (also called class variables) only exist in the class they are defined in.
They are not instantiated when an instance of the class is created.In other words, the values of these variables are not a part of the state of any object.


Eg:
public class Inner_class1{
private static final String Name = "Robert";
private static String Surname = "D'Silva";
private int age = 25;

public static void main(String[] args) {
Inner_class class1 = new Inner_class();
System.out.println(Inner_class.Surname); //should o/p D'Silva
System.out.println(class1.Surname); //should o/p D'Silva
class1.Surname = "part";
System.out.println(class1.Surname); //what should be the o/p.Why?
System.out.println(Inner_class.Surname); //what should be the o/p.Why?
}
}

Request Help to clear my doubts as I'm not able to proceed further reading.
Phillipe Rodrigues
Ranch Hand

Joined: Oct 30, 2007
Posts: 165
please neglect the previous eg:with correection done

public class Inner_class{
private static final String Name = "Robert";
private static String Surname = "D'Silva";
private int age = 25;

public static void main(String[] args) {
Inner_class class1 = new Inner_class();
System.out.println(Inner_class.Surname); //should o/p D'Silva
System.out.println(class1.Surname); //should o/p D'Silva
class1.Surname = "part";
System.out.println(class1.Surname); //what should be the o/p.Why?
System.out.println(Inner_class.Surname); //what should be the o/p.Why?
}
}
Chetan Parekh
Ranch Hand

Joined: Sep 16, 2004
Posts: 3636
Originally posted by neel mehra:

System.out.println(class1.Surname); //what should be the o/p.Why?
System.out.println(Inner_class.Surname); //what should be the o/p.Why?


In both the cases output will be "part". It doesn't matter you access static variable as Class.Variable_Name or Object.Variable_Name, but both are pointing to the same variable.
[ March 05, 2008: Message edited by: Chetan Parekh ]
Anubhav Anand
Ranch Hand

Joined: May 18, 2007
Posts: 341

Originally posted by Chetan Parekh:


In both the cases output will be "part". It doesn't matter you access static variable as Class.Varible_Name or Object.Varible_Name, but are pointing to the same varible.



Just wanted to add to Chetan's reply. As said that Static variables are not specific to an object. So when here you modify the value of surname to "part" then all future objects will get the value of surname as "part".

What I mean is if you make another new object of Inner_class and say


This will output : part
Phillipe Rodrigues
Ranch Hand

Joined: Oct 30, 2007
Posts: 165
But now if I do the Below:subclassing the previous class Inner_class


public class Static_nested_class_test extends Inner_class {
private static final String Name = "Robert";
private int age = 25;
public static void main(String[] args) {
Static_nested_class class1 = new Static_nested_class();
Inner_class inn = new Inner_class();
System.out.println(Inner_class.Surname); //what is the o/p.why?
System.out.println(inn.Surname); //what is the o/p.why?
}
}
Chetan Parekh
Ranch Hand

Joined: Sep 16, 2004
Posts: 3636
Compilation error. Static varibles of Inner_class are private.
[ March 05, 2008: Message edited by: Chetan Parekh ]
Phillipe Rodrigues
Ranch Hand

Joined: Oct 30, 2007
Posts: 165
With corrections:
public class Inner_class1{
private static final String Name = "Robert";
public static String Surname = "D'Silva";
private int age = 25;

public static void main(String[] args) {
Inner_class1 class1 = new Inner_class1();
System.out.println(Inner_class1.Surname); //should o/p D'Silva......(1)
System.out.println(class1.Surname); //should o/p D'Silva.......(2)
class1.Surname = "part";
System.out.println(class1.Surname);.........(3)
System.out.println(Inner_class1.Surname);.......(4)
}
}


mport oreilly.hcj.nested.Inner_class;


public class Static_nested_class_test2 extends Inner_class1 {
private static final String Name = "Robert";
private int age = 25;
public static void main(String[] args) {
Static_nested_class_test2 class1 = new Static_nested_class_test2();
Inner_class1 inn = new Inner_class1();
System.out.println(Inner_class.Surname);//o/p?.why?..(5)
System.out.println(inn.Surname);//o/p.Why? ....(6)
}
}
Phillipe Rodrigues
Ranch Hand

Joined: Oct 30, 2007
Posts: 165
Static members belong to the class in which they are declared, and are not part of any instance of the class.
with reference to above it means:


public class Inner_class1{
public static String Surname = "D'Silva";

public static void main(String[] args) {
Inner_class1 class1 = new Inner_class1();
System.out.println(Inner_class1.Surname);
System.out.println(class1.Surname);


with the above 2 statements executing they refer to original value of surname.

class1.Surname = "part";
System.out.println(class1.Surname);
System.out.println(Inner_class1.Surname);


now the class and the objects created refer to changes value.

But when subclassed they subclass should o/p the changed value(i.e part).why is it so?
Anubhav Anand
Ranch Hand

Joined: May 18, 2007
Posts: 341

Originally posted by Chetan Parekh:


It doesn't matter you access static variable as Class.Variable_Name or Object.Variable_Name, but both are pointing to the same variable.

[ March 05, 2008: Message edited by: Chetan Parekh ]


Hope you got Chetan's comment @ Neel.

Both 5&6 will output : D'Silva

As, the value D'Silva is same for all objects of the class Inner_class1.
It doesn't matter. I don't understand wht you want to get by this subclassing.
Anubhav Anand
Ranch Hand

Joined: May 18, 2007
Posts: 341

Originally posted by neel mehra:
But when subclassed they subclass should o/p the changed value(i.e part).why is it so?


The main method of your base/parent class(Inner_class1) was never called when you executed the main method of Static_nested_class_test2. So, the value of Surname was never changed to part. Thus, main method of Static_nested_class_test2 outputs as : D'Silva

Hope that solves the query.
[ March 05, 2008: Message edited by: Anubhav Anand ]
Phillipe Rodrigues
Ranch Hand

Joined: Oct 30, 2007
Posts: 165
Want to know as this question was asked during an interview.i gave same ans as told by chetan. but when executed the below 2 class the o/p at the
subclass gave result:
D Silva
D'Silva
where as in superclass it gave result:
D Silva.
D Silva.
part.
part
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: difference between object and instance
 
Similar Threads
Spot the difference
Instance Vs Class variables
Variables in Java
What's the difference between instance variable and class variable?
When to use Instance vs Local variables