• Post Reply Bookmark Topic Watch Topic
  • New Topic

static objects -  RSS feed

 
donald rieck
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The object references are static ( meaning no instance required );

If I do this (and it runs) :

class StaticObjects
{
public static Larry l = new Larry();
public static Curly c = new Curly();
public static Moe m = new Moe();

public static void main(String[] args)
{
l.dostuff();
c.dostuff();
m.dostuff();
}
}

class Larry
{
public static void dostuff() {
System.out.println("ahu gezzzzz");
}
}

class Curly
{
public static void dostuff() {
System.out.println("wuuw wuw wuwww");
}
}

class Moe
{
public static void dostuff() {
System.out.println("come onnnnnn");
}
}

What gives? Static type objects what, happens on the heap?
Does "new" can be put anywhere?
Sanity check


 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not exactly sure what your question is.

Static means, in this case, that the same references l, m, and c are available to all instances of StaticObjects. An instance of the class StaticObjects does not get its own copy of a Larry, Curly, and Moe reference.

But a static reference can refer to an instance of a class.
 
donald rieck
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about another class? Does it have to create instances first of
Larry, Curly, and Moe before l, c, and m have references, afterall
they are static.

:roll:
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whether they are static or not, they have to be assigned to point to actual objects before you can use them.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by donald rieck:
How about another class? Does it have to create instances first of
Larry, Curly, and Moe before l, c, and m have references, afterall
they are static.

:roll:


You could call Larry.doStuff() without creating an instance of Larry. Calling a static method like doStuff() on an instance (e.g. l.doStuff()) is generally considered bad form. That it's even allowed is probably a mistake in hindsight, but it's too late to change now.



If you're asking whether or not you can use the static variables l, c and m without actually assigning instances of Larry, Curly and Moe to those variables then that's already been answered. You don't need instances of Larry, Curly and Moe to call their static methods though.
[ October 27, 2006: Message edited by: Ken Blair ]
 
donald rieck
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It see it, what you guys have described.

Thank you guys for the sanity check!

It was the flow just made me; well, have to ask.


 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!