?
Solved

plz explain about static references to objects

Posted on 2009-07-09
4
Medium Priority
?
260 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 1000 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:Ken Clement
Ken Clement earned 1000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month10 days, 12 hours left to enroll

765 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