Solved

plz explain about static references to objects

Posted on 2009-07-09
4
253 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now