Solved

plz explain about static references to objects

Posted on 2009-07-09
4
255 Views
Last Modified: 2013-11-23
Suppose i have this code:
class A{
static A a;
}
and A a1 = new A();
and a1.a = new A();

and A a2 = new A();
and a2.a = new A();

now a1.a and a2.a should actually point to the same thing coz they belong to one class but since they can also be referenced by instance of class, they appear to point to different objects, so it means a1.a and a2.a is actually A.a so how can they point to different objects. Please explain this?
0
Comment
Question by:SunScreenCert
4 Comments
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24819479
A.a is a single, global variable, with class scope.

A static member is a single variable, not specific to an object instance. Instance variables, on the other hand, are stored in each object instance, each object has a copy.

0
 
LVL 92

Expert Comment

by:objects
ID: 24819484
no they would point to the same object, and the second assignment replaces the first.
Accessing a static via a object is undesirable though as it causes the confusion you are having, should always use A.a
0
 
LVL 7

Accepted Solution

by:
nikhilmenon earned 250 total points
ID: 24820645
Well let's see. The member variable 'a' belongs to the entire CLASS and NOT to any specific instance of that class.

Both a1 and a2 are two different instances/objects of type A. Since the reference 'a' is static, it belongs to the entire class. Therefore, both a1 and a2 will contain only one reference 'a' and specifying a1.a or a2.a does not make a difference. In fact, some IDEs throw up a warning saying a static member is being referenced in a non-static way if you specify a static reference this manner.

When you say a1.a = new A(), you are essentially making this static reference point to this new object that is created.

Then, when you say a2 = new A(); a2 already contains the reference 'a' which points to the object you created earlier in the previous statement (a1.a = new A() )

The final statement, a2.a = new A(), makes both a1.a and a2.a point to this new object being created.

Hence they will BOTH point to the SAME object and NOT a different one.
0
 
LVL 2

Assisted Solution

by:KenClement
KenClement earned 250 total points
ID: 24823856
To expand a bit on nikhilmesnon's comment above:

The statement

    if (a1.a == a2.a)
        System.out.println ("same");
    else
        System.out.println ("different");

Will ALWAYS result in the output "same", though you might get the warning about a static member referenced in a non-static way.
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question