Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

static object refrences and non static object refrences

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the difference between the static object references and the non-static object references ?
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Put the moon back where you found it! We need it for tides and poetry and stuff. Like this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic