• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Urgent Help.........

 
Phillipe Rodrigues
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
}
}
 
Marilyn de Queiroz
Sheriff
Posts: 9066
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is it urgent?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15441
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Anubhav Anand
Ranch Hand
Posts: 341
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15441
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Phillipe Rodrigues
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15441
41
Android IntelliJ IDE Java Scala Spring
 
Anubhav Anand
Ranch Hand
Posts: 341
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
could someone explain the reason why this causes the stack overflow. thanks.

n.
 
Anubhav Anand
Ranch Hand
Posts: 341
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic