Solved

plz explain about static references to objects

Posted on 2009-07-09
4
258 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
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:
Suggested Courses

737 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