[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

plz explain about static references to objects

Posted on 2009-07-09
4
Medium Priority
?
261 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 This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

649 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