This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static object refrences and non static object refrences

 
shveta gupta
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the difference between the static object references and the non-static object references ?
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you post such a question as this please consider whether it is SCJP specific or a question about a general aspect of Java. Just because you are studying for SCJP does not mean that all your questions need go here in the SCJP forum. So I am moving it to Java In General (Beginner).
 
Mike Noel
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The term "static object reference" doesn't make sense to me (maybe it's too early in the morning) because using a static member means that there is no object involved. Maybe what you are asking is what's the different between accessing a static method vs. accessing a non-static member.

Non-static methods are called using a object reference. For example:


In this case we create a new object, myObject, that is an instance of the class MyClass. After creating the object we call the method myMethod using the object to access it.

Static methods are called using the Class name instead of an object. For example:


In this case notice that we didn't need to create an instance of the MyClass class in order to call the method. We just called it using the class name.

The same applies to accessing public non-static and static fields in a class.

Hope this helps.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm guessing the original post meant a static member variable that is a reference to an object.

Mike's description of accessing static members by the class and non-static members by an instance is right on. The language syntax allows you to use a static member by an instance reference, but most of us try not to and some IDEs issue a warning.

Consider this example of one static field and one non-static field

You need an instance of ThingOne to refer to field one: myInstance.one. And every time you create a new MyClass you'll create a new ThingOne. So each instance of MyClass has its own instance of ThingOne to play with.

You can refer to ThingTwo via the class: MyClass.two. Because the class holds this field and not any instance, when you create a new instance of MyClass, you don't get a new instance of ThingTwo. Any time you reference MyClass.two you'll reference the same instance of ThingTwo. You have to be aware that all instances of MyClass share a common instance of ThingTwo or you can get very confused about who is changing what.

Any of that help?
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok i have a program that has a static member data array "Node [] theNodes", that holds 60 references to Node objects..

and each Node has prevNum, nextNum, prevHrs, nextHrs.
and int EmployeeNum, and double EmployeeHrs.

the reason the array is static, is so i can do the following?

theNodes[i].nextNum;

am I right?

thx,
justin
[ July 15, 2006: Message edited by: Justin Fox ]
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Justin Fox:
ok i have a program that has a static member data array "Node [] theNodes", that holds 60 references to Node objects..

and each Node has prevNum, nextNum, prevHrs, nextHrs.
and int EmployeeNum, and double EmployeeHrs.

the reason the array is static, is so i can do the following?

theNodes[i].nextNum;

am I right?

thx,
justin

[ July 15, 2006: Message edited by: Justin Fox ]

The reference doesnt have to be static for you to access it in that manner, assuming of course that nextNum is in scope.
[ July 15, 2006: Message edited by: Garrett Rowe ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic