• Post Reply Bookmark Topic Watch Topic
  • New Topic

strange output  RSS feed

 
Rrohit rakesh upadhyay
Ranch Hand
Posts: 94
1
Java Notepad Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when i executed the following code,i got "private method" as the output.But whe i removed those bacl slashes from "Pri p=new Pri",the output was " at Pri.<init><M.java:4>" on multiple lines.Why is it like that?


 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rrohit rakesh upadhyay wrote:when i removed those bacl slashes from "Pri p=new Pri",the output was " at Pri.<init><M.java:4>" on multiple lines.Why is it like that?


The message is a stack trace. When you don't catch exceptions, the default action is to print the stack trace upon an Exception/Error from the main() method. The error is either an out of memory error, or a stack overflow, depending on which resource you ran out of first.

And of course, the issue is that you have an infinitely recursive initialization. Instantiating a Pri instance will require the instantiation of another Pri instance.

Henry
 
Rrohit rakesh upadhyay
Ranch Hand
Posts: 94
1
Java Notepad Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After i instantiate pri the constructer is invoked,but after that why is pri getting instantiated again.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rrohit rakesh upadhyay wrote:After i instantiate pri the constructer is invoked,but after that why is pri getting instantiated again.

Because when you create a new instance of the Pri class it initializes the instance variable 'p' which causes a new instance of Pri to be created which has it's own instance variable 'p' which when initialized creates a new instance of Pri which has it's own instance variable 'p' which when initialized creates a new instance of Pri which has it's own instance variable 'p' which when initialized creates a new instance of Pri which has it's own instance variable 'p' which when initialized creates a new instance of Pri etc etc until you run out of resources as Henry explained.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rrohit rakesh upadhyay wrote:After i instantiate pri the constructer is invoked,but after that why is pri getting instantiated again.


When you instantiate the Pri object, one of the things that needs to be done is to instantiate its instance variables. One of those instances variables is a Pri object. When you instantiate the Pri object for that instance variable, one of the things that needs to be done is to instantiate its instance variables. One of those instances variables is a Pri object. When you instantiate the Pri object for that instance variable, one of the things that needs to be done is to instantiate its instance variables. One of those instances variables is a Pri object. When you instantiate the Pri object for that instance variable, one of the things that needs to be done is to instantiate its instance variables. One of those instances variables is a Pri object. When you instantiate the Pri object for that instance variable, one of the things that needs to be done is to instantiate its instance variables. One of those instances variables is a Pri object. When you instantiate the Pri object for that instance variable, one of the things that needs to be done is to instantiate its instance variables. One of those instances variables is a Pri object. When you instantiate the Pri object for that instance variable, one of the things that needs to be done is to instantiate its instance variables. One of those instances variables is a Pri object. When you instantiate the Pri object for that instance variable, one of the things that needs to be done is to instantiate its instance variables. One of those instances variables is a Pri object. When you instantiate the Pri object for that instance variable, one of the things that needs to be done is to instantiate its instance variables. One of those instances variables is a Pri object. When you instantiate the Pri object for that instance variable, one of the things that needs to be done is to instantiate its instance variables. One of those instances variables is a Pri object. When you instantiate the Pri object for that instance variable, one of the things that needs to be done is to instantiate its instance variables. One of those instances variables is a Pri object. When you instantiate the Pri object for that instance variable, one of the things that needs to be done is to instantiate its instance variables. One of those instances variables is a Pri object. When you instantiate the Pri object for that instance variable, one of the things that needs to be done is to instantiate its instance variables. One of those instances variables is a Pri object. When you instantiate the Pri object for that instance variable, one of the things that needs to be done is to instantiate its instance variables. One of those instances variables is a Pri object. When you instantiate the Pri object for that instance variable, one of the things that needs to be done is to instantiate its instance variables. One of those instances variables is a Pri object. When you instantiate the Pri object for that instance variable, one of the things that needs to be done is to instantiate its instance variables. One of those instances variables is a Pri object. ... blah blah blah ... do you really need me to continue? ...

[EDIT: beaten to the response by a minute. I guess I went a little too deep in the recursive explanation!]

Henry
 
Rrohit rakesh upadhyay
Ranch Hand
Posts: 94
1
Java Notepad Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my dear Tony and Henry ,thank you very much for your detailed replies ,but if i write the same "Pri p =new Pri" in the main method everything goes normally
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct. And you do understand why, right?

Henry
 
Rrohit rakesh upadhyay
Ranch Hand
Posts: 94
1
Java Notepad Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually i do not.Please help
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Do you know the difference between a local variable and an instance variable?
 
Rrohit rakesh upadhyay
Ranch Hand
Posts: 94
1
Java Notepad Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Local variables are those which are declared inside a method or as method parameter(s),the cannot be used outside their respective methods.

Instance variables are those which are declared inside a class but outside a method and can be used by any method.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rrohit rakesh upadhyay wrote:
Instance variables are those which are declared inside a class but outside a method and can be used by any method.


Almost. What you described can be used for static variables too.


Regardless... this was not just a random question. I presumed that you understood the detailed response (as you thanked us for it). Now you got a hint, of what is the difference between the two cases. Hopefully, you should be able to figure it out.

If you are still having a problem understanding it, then tell us everything that you understood from the detailed response. And everything that you think is happening that you think that the second case should also not work. Hopefully, from you descriptions, we can get an idea of your confusion, and give you a hint in the right direction.

Henry
 
Rrohit rakesh upadhyay
Ranch Hand
Posts: 94
1
Java Notepad Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok,i think i figured it out.Here is what i think -

when JVM loads class Pri it encounters "Pri p=new Pri" which causes the JVM to load Pri again,when JVM loads class Pri it encounters "Pri p=new Pri" which causes the JVM to load Pri again,when JVM loads class Pri it encounters "Pri p=new Pri" which causes the JVM to load Pri again,when JVM loads class Pri it encounters "Pri p=new Pri" which causes the JVM to load Pri again,when JVM loads class Pri it encounters "Pri p=new Pri" which causes the JVM to load Pri again,when JVM loads class Pri it encounters "Pri p=new Pri" which causes the JVM to load Pri again,when JVM loads class Pri it encounters "Pri p=new Pri" which causes the JVM to load Pri again,when JVM loads class Pri it encounters "Pri p=new Pri" which causes the JVM to load Pri again,when JVM loads class Pri it encounters "Pri p=new Pri" which causes the JVM to load Pri again,when JVM loads class Pri it encounters "Pri p=new Pri" which causes the JVM to load Pri again,when JVM loads class Pri it encounters "Pri p=new Pri" which causes the JVM to load Pri again,when JVM loads class Pri it encounters "Pri p=new Pri" which causes the JVM to load Pri again,when JVM loads class Pri it encounters "Pri p=new Pri" which causes the JVM to load Pri again,when JVM loads class Pri it encounters "Pri p=new Pri" which causes the JVM to load Pri again,when JVM loads class Pri it encounters.

Am i right?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rrohit rakesh upadhyay wrote:Am i right?

I think that's pretty much what Henry said (or part of it) ... and he's usually right.

Winston
 
Rrohit rakesh upadhyay
Ranch Hand
Posts: 94
1
Java Notepad Oracle
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston,i am going to take that as a yes .
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rrohit rakesh upadhyay wrote:
when JVM loads class Pri it encounters "Pri p=new Pri" which causes the JVM to load Pri again,when JVM loads class Pri it encounters ...


To be pedantic, the term "load class" generally refers to the class loader. And the Pri class file is only loaded once.

Henry
 
Rrohit rakesh upadhyay
Ranch Hand
Posts: 94
1
Java Notepad Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok,thanks
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!