aspose file tools*
The moose likes Beginning Java and the fly likes static object refrences and non static object refrences Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "static object refrences and non static object refrences" Watch "static object refrences and non static object refrences" New topic
Author

static object refrences and non static object refrences

shveta gupta
Greenhorn

Joined: Jul 12, 2006
Posts: 4
What is the difference between the static object references and the non-static object references ?
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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).


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Mike Noel
Ranch Hand

Joined: Dec 15, 2005
Posts: 108
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.


Mike Noel
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
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 ]

You down with OOP? Yeah you know me!
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
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 ]

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static object refrences and non static object refrences