wood burning stoves*
The moose likes Beginning Java and the fly likes Functions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Functions" Watch "Functions" New topic
Author

Functions

Amit Sethi
Ranch Hand

Joined: May 30, 2006
Posts: 45
Q. Class contain Instance variable, class variable, methods, static methods. Are these simple methods, class method(only one time space is given to methods) or instance method(space is given for every object).



PLS help me.
regards
Amit
Jan Groth
Ranch Hand

Joined: Feb 03, 2004
Posts: 456
can you give an example for what you call a"simple method"? your post is kind of hard to understand... :-)

cheers,
jan
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37980
    
  22
If you don't say "class variable":-

static methods and variables:-
  • One copy in memory for each class
  • Called by myClass.myStaticMethod();
  • Every instance of that class has access to the same copy of the static method or variable
  • A static method does not have access to any instance variables, or instance methods, or the this and super keywords.

  • instance methods and variables:-
  • All methods and variables not called "static" are instance methods or variables. Is that what you mean by "simple methods?"
  • There is one copy of each instance variable in memory for each instance of the class, so there may be many of them.
  • You gain access to instance methods and variables by calling myObject.myMethod();
  • Each instance method has access to any static members of its class, but it is always the same static member.
  • All instance methods have access to all other instance methods and instance variables from their own object. This usually represents different data in different objects.
  • Instance methods have access to the this and super keywords.
  • Does that help at all?

    CR
    [ May 31, 2006: Message edited by: Campbell Ritchie ]
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 37980
        
      22
    . . . And please think of titles for your threads which tell readers what they are about. "Functions" is too vague.
    Ryan McGuire
    Ranch Hand

    Joined: Feb 18, 2005
    Posts: 1006
        
        3
    Originally posted by Amit Sethi:
    Q. Class contain Instance variable, class variable, methods, static methods. Are these simple methods, class method(only one time space is given to methods) or instance method(space is given for every object).


    I agree with what Campbell has said, but let me go back to one thing in the original question.

    Instance methods are not in memory multiple times. Even in the most complex multi-threaded application with huge number of objects, the executable code for the method is in memory just once.* Multiple threads might be executing the same methods simultaneously, but each one would be using the same executable code with (most likely) different variables/objects.

    Of course each object has its own memory space allocated for instance variables.


    * If I understand correctly, you could have an app that has multiple class loaders, in which case the code for a class's methods might be in memory more than once. In general, however, each class's code is loaded only once. (If I'm mistaken, someone please say so.)
    [ May 31, 2006: Message edited by: Ryan McGuire ]
    Amit Sethi
    Ranch Hand

    Joined: May 30, 2006
    Posts: 45
    Thanks a lot ...Jan,Campbell,Ryan

    What i got from ur answers is :

    simple methods are not given space for every object, they are given space only once like static methods but both are different by properties.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Functions
     
    Similar Threads
    Head First Java Book question
    hello... i need simple explanation for this......
    synchronized(ObjectInstance){ //satements } AND Polymorphism
    Variable Initialization
    do objects have seprate copies of the instance methods as they have separate copies