This discussion is archived
5 Replies Latest reply: Jun 5, 2011 3:40 PM by EJP RSS

java method byte code location

866541 Newbie
Currently Being Moderated
as we know that when we do new CustomerInfo(); class object associated with CustomerInfo will be created and byte code for the complete class stored somewhere in method area. My question is when we do new CustomerInfo().somemethod(); how jvm reaches to method byte code location as i can not see any any bytecode reference in method objects associated with CustomerInfo. Also in methoid area we just store the mapping of classname with classobject only but how jvm will get the method byte code to execute
  • 1. Re: java method byte code location
    796440 Guru
    Currently Being Moderated
    863538 wrote:
    as we know that when we do new CustomerInfo(); class object associated with CustomerInfo will be created
    No. The Class object is created once, when the class is loaded, which occurs when it is first referenced. It does not get created on every call to new.
    and byte code for the complete class stored somewhere in method area. My question is when we do new CustomerInfo().somemethod(); how jvm reaches to method byte code location as i can not see any any bytecode reference in method objects associated with CustomerInfo.
    The new operator returns a reference to the object just created. We then use that reference to invoke the method. It's no different than if we had assigned the results of new to a variable and then used the reference held in that variable to access the object. Internally to the JVM, a reference to an object includes a path to get to its class definition, but exactly how that is implemented is not specified, so individual implementations can do it however they want.
    Also in methoid area we just store the mapping of classname with classobject only
    What? In the method area, the entire class definition is stored, including its method bodies.
  • 2. Re: java method byte code location
    EJP Guru
    Currently Being Moderated
    when we do new CustomerInfo(); class object associated with CustomerInfo will be created
    No it won't. The class will be loaded if necessary when a class that references it is loaded.
    when we do new CustomerInfo().somemethod(); how jvm reaches to method byte code location as i can not see any any bytecode reference in method objects associated with CustomerInfo.
    That doesn't make sense. It wouldn't help if there was. You would have to find the byte code before you could find the byte code. The byte code location is store with the rest of the information about the class.
    Also in method area we just store the mapping of classname with classobject only
    In the constant pool, but there's no 'just' or 'only' about it. Lots of things are stored there.
    how jvm will get the method byte code to execute
    Via a mapping from the class object to its byte code in the constant pool.

    I suggest you read Chapter 5 of the Java Virtual Machine Specification where all this is specified. Your present information is just meaningless guesswork on somebody's part.
  • 3. Re: java method byte code location
    866541 Newbie
    Currently Being Moderated
    when we do CustomerInfoclass object associated with CustomerInfo will be created
    No it won't. The class will be loaded if necessary when a class that references it is loaded.

    As per my understanding class will be loaded in any one of the following cases :-

    1)CustomerInfo.someStaticMethod();
    2)new CustomerInfo().someInstanceMethod();
    3)SomeChildClass extends CustomerInfo

    But it does not get loaded when we just refer it in some method like
    CustomerInfo custInfo;

    correct?



    when we do new CustomerInfo().somemethod(); how jvm reaches to method byte code location as i can not see any any bytecode reference in method objects associated with CustomerInfo.
    That doesn't make sense. It wouldn't help if there was. You would have to find the byte code before you could find the byte code. The byte code location is store with the rest of the information about the class.

    Thanks for pointing me to Chapter 5 of the Java Virtual Machine Specification.
    After reading this i got know the all byte code gets(method) stored in rutime constant pool under method_info structure table which gets created during class loading. JVM resolves it while dynamic linking where it replaces the sybolic references of called method with direct references where the byte code is actually lying.


    Please let me know if it is correct?

    Edited by: 863538 on Jun 5, 2011 4:47 AM
  • 4. Re: java method byte code location
    796440 Guru
    Currently Being Moderated
    863538 wrote:
    when we do CustomerInfoclass object associated with CustomerInfo will be created
    No it won't. The class will be loaded if necessary when a class that references it is loaded.

    As per my understanding class will be loaded in any one of the following cases :-

    1)CustomerInfo.someStaticMethod();
    2)new CustomerInfo().someInstanceMethod();
    3)SomeChildClass extends CustomerInfo

    But it does not get loaded when we just refer it in some method like
    CustomerInfo custInfo;
    It gets loaded the first time it's referenced. Only once. You seem to be under the impression it gets loaded every time one of those things happens.

    In the case where we simply declare a local variable and never use it, I don't know if the class is necessarily loaded, nor do I care, since that situation never arises in real code, and if it did, we'd never know the difference either way.
  • 5. Re: java method byte code location
    EJP Guru
    Currently Being Moderated
    As per my understanding class will be loaded in any one of the following cases
    It gets loaded the first time it is needed. Simple as that. The correct cases will be found in the JVM specification, or can be inferred fom it. I don't see any point in making your own list, or of asking other people to check it for correctness. Same goes for your paragraph about method code. What you say or Jeff and I say is irrelevant. The specification is correct. Use it. Reference it. Don't mangle it into your own words.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points