Results 1 to 10 of 10
  1. #1
    s021126 is offline Member
    Join Date
    Aug 2009
    Posts
    4
    Rep Power
    0

    Default Concept about methods of Class

    Suppose there is a Class A , and inside class A ,there is a method called methodA
    If I create two objects using this class, will both of the methodA of object1 and object 2 occupy the same memory, nomatter the method is static or not?

    thxz=

  2. #2
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    6

    Default

    The question doesn't really make sense. When the class is loaded by the class loader as part of the program, the class code with its methods sits in memory and gets executed whenever class methods are called. Each class instance is a separate chunk of memory containing its own fields and variables (except for static fields which are shared by all instances), that is associated with the loaded class code. A method is just a part of the class code. It only gets loaded once when the application loads. Declaring a method static or non-static only affects how you can call the method and exactly which piece of code is actually run when you call it.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by s021126 View Post
    Suppose there is a Class A , and inside class A ,there is a method called methodA
    If I create two objects using this class, will both of the methodA of object1 and object 2 occupy the same memory, nomatter the method is static or not?

    thxz=
    Your question is quite mess-up here. Seems to me that you want to know either objects share the memory or not?

    Object is basically a blue-print of a class. Each time you initialize an object, memory each allocated separately. Not share them in between. Each objected deals with there members separately as well.

    If you can explain your question more clearly, we can provide more details.

  4. #4
    s021126 is offline Member
    Join Date
    Aug 2009
    Posts
    4
    Rep Power
    0

    Default

    thank for you help

    Is the same methods of different objects occupy seperate memory too?

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    You cannot have objects for methods. Because you cannot implement methods individually in Java. You had to have a container like class.

  6. #6
    s021126 is offline Member
    Join Date
    Aug 2009
    Posts
    4
    Rep Power
    0

    Default

    Sorry, Is that means method in java must be static?

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

  8. #8
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    5

    Default

    All methods in java belong to a class.

    A method is set of instructions. That set of instructions lives in memory only in one place, for both static or non-static methods, no matter how many times you create on object of that class.

    A non-static method has access to member variables of an object but a static method does not. The advantage to using a static method is that you don't need to create an instance of the class to use that method. That can be useful for helper methods.

    Java Code:
    long round = Math.round(.999); //round is a static method of class Math
    Last edited by mrmatt1111; 08-24-2009 at 05:58 AM.
    My Hobby Project: LegacyClone

  9. #9
    s021126 is offline Member
    Join Date
    Aug 2009
    Posts
    4
    Rep Power
    0

    Default

    Thank you.
    I understand now!

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

Similar Threads

  1. Replies: 8
    Last Post: 03-03-2009, 08:28 AM
  2. Declare methods in a class
    By Adiel224 in forum New To Java
    Replies: 5
    Last Post: 09-19-2008, 10:38 AM
  3. Abstract Class with Static Methods
    By bugger in forum New To Java
    Replies: 7
    Last Post: 09-05-2008, 12:20 AM
  4. Class Reflection: Showing methods
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 08:11 PM
  5. Getting methods of a Class - Reflection
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-15-2007, 03:18 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •