aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question on static Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question on static" Watch "Question on static" New topic
Author

Question on static

Sean Casey
Ranch Hand

Joined: Dec 16, 2000
Posts: 625
What would be the result of attempting to compile and run the following program?
class MyClass{
static MyClass ref;
String[] arguments;
public static void main(String args[]){
ref= new MyClass();
ref.func(args);
}
public void func(String[] args){
ref.arguments= args;
}
}
The answer says that the program will compile and run successfully. My question is how can ref be used when it is declared as: static MyClass ref? Doesn't this make it a static variable, thus a class variable and not being able to be instantiated? Can someone please explain this because I have no clue how this can compile and run successfully. Thanks.
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

Hi Sean -
A "static variable," or class variable is a reference that belongs to the class. Each MyClass object can use that shared reference, but they don't get one of their own. Static doesn't mean it's un-instantiable, but it does "exist" in memory before a MyClass object ever gets created.
Does that help?
-----------------
Michael Ernest, co-author of:
The Complete Java 2 Certification Study Guide

[This message has been edited by Michael Ernest (edited December 22, 2000).]


Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
Sean Casey
Ranch Hand

Joined: Dec 16, 2000
Posts: 625
I understand the definition to a static variable. Is that to say that each new class can create another new class using: static MyClass ref; since it is a "shared" reference. For some reason it just doesn't seem to make sense to me.
Sean Casey
Ranch Hand

Joined: Dec 16, 2000
Posts: 625
Okay Michael, I understand it now. I did what I should've done in the first place. I created a program to demonstrate it.
class MyClass{
static MyClass ref;
String[] arguments;
static int counter;
public int x;
public MyClass(int x){
counter += x;}
public MyClass(){};


public static void main(String args[]){

ref= new MyClass();

ref.func(args);
ref= new MyClass(1);

ref= new MyClass(1);
ref= new MyClass(1);
ref= new MyClass(1);

System.out.println("Counter = " + counter);
}

public void func(String[] args){
ref.arguments= args;
}
}
I think this demonstrates it. When this is compiled and run counter = 4. Thanks for your help. Please let me know if any of this doesn't make any sense. Thanks.
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292


You got it just fine. Hopefully my explanation didn't sound as condescending as your response hinted.
Sean Casey
Ranch Hand

Joined: Dec 16, 2000
Posts: 625
Michael thanks for your help. I didn't mean to hint you were being condescending. It just took me a while to "get it" even though you had given me the right explanation. But, thanks again for your help.
anjan bhushan
Ranch Hand

Joined: Dec 12, 2000
Posts: 71
That's means one has created four object and has been reffered by
one static calss ref.So after the creation of fourt one 3 objects are already ready for gc(If it is not collected by gc already.)
Am I right?
[This message has been edited by anjan bhushan (edited December 23, 2000).]


I am the most eligible bachelor in whole world, but only known in limited territory!!!
Digital Intoxication Blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question on static