Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Static initializer doesn't return value. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Static initializer doesn Watch "Static initializer doesn New topic
Author

Static initializer doesn't return value.

Ritesh raushan
Ranch Hand

Joined: Aug 29, 2012
Posts: 100
why Static initializer doesn't return value.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13875
    
  10

Suppose that it did, where would that return value then go to?

What do you think is calling the static initializer (and when)?


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

Joined: Aug 29, 2012
Posts: 100
Jesper de Jong wrote:Suppose that it did, where would that return value then go to?

What do you think is calling the static initializer (and when)?



to initailize static state and at the time of loading the class.

and i also want to know where memory alocated for final variable and return value.
i think final variable in heap and return value in stack. am i write.please explain this also.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7063
    
  16

Ritesh raushan wrote:to initailize static state and at the time of loading the class.

Fine, but that doesn't answer Jesper's question: what should it return, and where would it return it to?

and i also want to know where memory alocated for final variable and return value.
i think final variable in heap and return value in stack. am i write.please explain this also.

The best place to find out about all that stuff is the JLS

Java is a memory-managed language, so 99% of the time you create objects and let the JVM decide where they go and when they get removed. If this is just a mental exercise, by all means knock yourself out; but quite honestly, it won't help you to become a better programmer. In fact, it's more likely to side-track you from things that are more important.

Personally, I can't remember the last time I worried about where something goes (oops, tell a lie; it was my SCJP exam back in 2006).

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Ritesh raushan wrote:
i think final variable in heap and return value in stack. am i write.please explain this also.


All local variables always go on the stack, whether they are final or not.

All object and all their member variables, all class definitions, all constant pools, go on the heap. The only exception to this is that starting at some point in Java 6, the JVM spec apparently allows objects to be created on the stack of escape analysis can prove that they won't live beyond the method invocation.

But, as already pointed out, that knowledge is not generally useful to a Java developer. In 14 years of Java development, that knowledge has never once affected how I write my code.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
Which is why you won’t find such information in the JLS. It is an implementation detail, and as the last two people say, you don’t actually need to know it. Anyway, it can differ from implementation to implementation.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7063
    
  16

Campbell Ritchie wrote:Which is why you won’t find such information in the JLS...

Oops. Quite right. Sorry Ritesh. It'll be in the JVM specification.

Winston
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
Winston Gutkowski wrote: . . . It'll be in the JVM specification. . . .
It might not be there, either, if it is an implementation detail.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7063
    
  16

Campbell Ritchie wrote:It might not be there, either, if it is an implementation detail.

Well, it certainly defines the areas, but I have to admit, I couldn't be fagged to find out whether it specifies where specific things like return values are put. It feels like a "stacky" sort of thing, but I'm certainly no system programmer.

Winston
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
Winston Gutkowski wrote: . . . I couldn't be fagged to find out whether it specifies where specific things like return values are put. . . .
Winston
Quite right, too. You have better things to think about. I need to know that sort of thing when I am writing my compiler in FORTH, but ordinary users of Java don’t need that info at all.
Ritesh raushan
Ranch Hand

Joined: Aug 29, 2012
Posts: 100
Jeff Verdegan wrote:
Ritesh raushan wrote:
i think final variable in heap and return value in stack. am i write.please explain this also.


All local variables always go on the stack, whether they are final or not.

All object and all their member variables, all class definitions, all constant pools, go on the heap. The only exception to this is that starting at some point in Java 6, the JVM spec apparently allows objects to be created on the stack of escape analysis can prove that they won't live beyond the method invocation.

But, as already pointed out, that knowledge is not generally useful to a Java developer. In 14 years of Java development, that knowledge has never once affected how I write my code.


sorry to say.i think only instance variable go onto the heap and one pointer in heap that points method area where all information about class(all method) is stored.
and i want to know where final instance variable is stored and one heap created for one application mean if i create two object of same class then both object share heap or created two heap for two object.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
Implementation details.

How do you think you could create two objects? Would you really create two heaps?
Ritesh raushan
Ranch Hand

Joined: Aug 29, 2012
Posts: 100
Campbell Ritchie wrote:Implementation details.

How do you think you could create two objects? Would you really create two heaps?


no Cambell sir i just asked a question .
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3950
    
  17

Ritesh raushan wrote:sorry to say.i think only instance variable go onto the heap

You would mostly be wrong. The correct answer has been stated multiple times (implementation detail), and the most usual implementation has also already been described by Jeff.


Ritesh raushan wrote:one heap created for one application mean if i create two object of same class then both object share heap or created two heap for two object.

There is only one heap to think about. If you have two objects they have different locations inside the same heap. Even if they are two objects of the same type.


Steve
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Ritesh raushan wrote:
Jeff Verdegan wrote:
Ritesh raushan wrote:
i think final variable in heap and return value in stack. am i write.please explain this also.


All local variables always go on the stack, whether they are final or not.

All object and all their member variables, all class definitions, all constant pools, go on the heap. The only exception to this is that starting at some point in Java 6, the JVM spec apparently allows objects to be created on the stack of escape analysis can prove that they won't live beyond the method invocation.

But, as already pointed out, that knowledge is not generally useful to a Java developer. In 14 years of Java development, that knowledge has never once affected how I write my code.


sorry to say.i think only instance variable go onto the heap and one pointer in heap that points method area where all information about class(all method) is stored.


According the the JVM spec, the method area "is logically part of the heap." It doesn't say anything about a pointer in the heap pointing to the method area. Unless they've changed it for Java 7. And if they did, as others have pointed out, it wouldn't matter to Java programmers. That stuff only matters if you're implementing a JVM.

and i want to know where final instance variable is stored


As already stated: All instance variables (non-static member variables), final or not, are stored on the heap. They are stored as part of their object. And all class variables (static member variables), final or not, are also stored on the heap, as part of the class definition in the method area. (Unless the JVM is using escape analysis and putting some objects on the stack. Then those objects' instance variables, final or no will be on the stack, or possibly in registers.)

And all local variables, final or not, are stored on the stack.

http://docs.oracle.com/javase/specs/jvms/se5.0/html/Overview.doc.html#1732

And again, none if this matters for how you write your Java code, though I do think it's useful to have at least a rough understanding of this stuff.

and one heap created for one application mean if i create two object of same class then both object share heap or created two heap for two object.


There's one heap for each JVM instance. So if your program creates 2 objects, then those 2 objects are inhabiting the same heap.
Ritesh raushan
Ranch Hand

Joined: Aug 29, 2012
Posts: 100
thanks jeff sir.i was wrong
 
wood burning stoves
 
subject: Static initializer doesn't return value.
 
Similar Threads
Static method
exceptions thrown from initializer blocks
Return Statement..
Constructor vs instance initializer
grammar help: what is this structure?