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

Urgent Help.........

Phillipe Rodrigues
Ranch Hand

Joined: Oct 30, 2007
Posts: 165
What does the below 2 code samples mean?Is prog 1proper way to initiate a class/prog 2?why?

Prog 1:
public class start{

start st = new start();
}

----------------------------------------------------------------------------
Prog 2:
public class start{

public static void main(String[] args){
start st = new start();
}
}


Thanks,
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
Why is it urgent?


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14157
    
  19

Please use a meaningful subject line instead of "Urgent Help".

Try compiling and running those two code snippets. What happens when you try this? Can you explain the output?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Anubhav Anand
Ranch Hand

Joined: May 18, 2007
Posts: 341

Well, when you make a variable at class level then it becomes a member/instance variable. This means every object will have a specific copy of that varibale.

Now, it depends entirely on need to use either code snippets. But, still 1st snippet even when it is legal is a bad practice and should not be followed. If required you can make the reference and initilize it inside a method(if required). Moreover, it is also a memory loss if you make a new variable at instance level and don't use it afterwards

PS. Quick tip: follow sun's rule for declarations. A class name can but shouldn't start in lowercase.
[ April 08, 2008: Message edited by: Anubhav Anand ]
Chandrasekhar Mangipudi
Ranch Hand

Joined: Jan 29, 2006
Posts: 118
HI.

Prog 1:
public class start{

start st = new start();
}


In Program1: You have defined and intialized "start" class name as member variable to its same class. This type of declaration is not recommended in java.
----------------------------------------------------------------------------
Prog 2:
public class start{

public static void main(String[] args){
start st = new start();
}
}

In Prog2:

You have declared and initialized Start class as local variable to main method. So it is available to main method only not outside, it will create
object for start class one time.
[ April 08, 2008: Message edited by: Chandrasekhar Mangipudi ]

Thanks & Regards,
ChandraSekharMangipudi
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14157
    
  19

Originally posted by Anubhav Anand:
Now, it depends entirely on need to use either code snippets. But, still 1st snippet even when it is legal is a bad practice and should not be followed.

Originally posted by Chandrasekhar Mangipudi:
In Program1: You have defined and intialized "start" class name as member variable to its same class. This type of declaration is not recommended in java.

Why is this bad practice that should not be followed, or why is this not recommended?

Do you understand what happens if you run this?

I recommend you try this out, see what happens, and try to explain what you see.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Originally posted by Jesper Young:

Do you understand what happens if you run this?

I recommend you try this out, see what happens, and try to explain what you see.


Of course, he can't run it, because it has no main(). Maybe try running this:



And then explain the result...


[Jess in Action][AskingGoodQuestions]
Phillipe Rodrigues
Ranch Hand

Joined: Oct 30, 2007
Posts: 165
What should the possible reason behind writing the below code?

public class Chack_pay_id {
public Chack_pay_id(){
new Chack_pay_id();
}
public static void main(String[] args){
System.out.println(new Chack_pay_id());
}

}
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14157
    
  19

Please don't double post.
Anubhav Anand
Ranch Hand

Joined: May 18, 2007
Posts: 341

Why is this bad practice that should not be followed, or why is this not recommended?

Do you understand what happens if you run this?

code:
--------------------------------------------------------------------------------

public class start { start st = new start();}

--------------------------------------------------------------------------------


I recommend you try this out, see what happens, and try to explain what you see.


Well, Indeed that was a big fault and I wrote a compilation oriented answer. Probably I had to induce some more if-buts...
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Originally posted by neel mehra:
What should the possible reason behind writing the below code?


None, except to prove a point about stack overflows. If you found this in production code somewhere, the coder didn't know what s/he was doing.
f. nikita thomas
Ranch Hand

Joined: Mar 02, 2008
Posts: 87
could someone explain the reason why this causes the stack overflow. thanks.

n.


Imagination is more important than knowledge "Albert Einstein"
Anubhav Anand
Ranch Hand

Joined: May 18, 2007
Posts: 341



Just to get a clearer view let's add a constructor to the given code. Now, as we know that every object of the class has it's own copy of the instance variables. What happens is when we say in the main method then it calls the default constructor(which if not provided by the user is provided by the compiler). Now, the constructor tries to first associate with the object it's own copy of instance variables. In our case the instance variable is . As this variable tries to itself call the constructor which is currently executing the constructor call goes into a recursive call. Thus, after some time when there are a lerge number of objects created which can't be held on the stack we get the StackOverflowError by the JVM.

Hope that helps.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Anubhav Anand is right.

It is because you instantiate a new object of the class. In the class each instance will have its own copy of instance variables. Here you have an instance variable of the same class, which is initialized to a brand new object by invoking the constructor -- the whole cycle repeats until the stack explodes by throwing StackOverFlow exception. An typical scenario of OutOfMemory we can say!


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
 
Don't get me started about those stupid light bulbs.
 
subject: Urgent Help.........