• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on static

 
Sean Casey
Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Sean Casey
Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 625
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You got it just fine. Hopefully my explanation didn't sound as condescending as your response hinted.
 
Sean Casey
Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic