Results 1 to 10 of 10
Thread: Concept about methods of Class
- 08-22-2009, 07:22 AM #1
Member
- Join Date
- Aug 2009
- Posts
- 4
- Rep Power
- 0
- 08-22-2009, 10:59 AM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
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.
- 08-22-2009, 03:06 PM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 08-23-2009, 03:01 PM #4
Member
- Join Date
- Aug 2009
- Posts
- 4
- Rep Power
- 0
thank for you help
Is the same methods of different objects occupy seperate memory too?
- 08-23-2009, 03:13 PM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
You cannot have objects for methods. Because you cannot implement methods individually in Java. You had to have a container like class.
- 08-24-2009, 05:32 AM #6
Member
- Join Date
- Aug 2009
- Posts
- 4
- Rep Power
- 0
Sorry, Is that means method in java must be static?
-
No, only methods declared static are static. All other methods are by default non-static (or "virtual").
- 08-24-2009, 05:56 AM #8
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
- 08-24-2009, 05:57 AM #9
Member
- Join Date
- Aug 2009
- Posts
- 4
- Rep Power
- 0
Thank you.
I understand now!
- 08-25-2009, 04:23 AM #10
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
And read more on Suns' tutorial. Well explained all those concepts.
Lesson: Classes and Objects (The Java™ Tutorials > Learning the Java Language)
Similar Threads
-
How to Cal methods within a method in the same class
By Manfizy in forum New To JavaReplies: 8Last Post: 03-03-2009, 08:28 AM -
Declare methods in a class
By Adiel224 in forum New To JavaReplies: 5Last Post: 09-19-2008, 10:38 AM -
Abstract Class with Static Methods
By bugger in forum New To JavaReplies: 7Last Post: 09-05-2008, 12:20 AM -
Class Reflection: Showing methods
By Java Tip in forum java.langReplies: 0Last Post: 04-23-2008, 08:11 PM -
Getting methods of a Class - Reflection
By Java Tip in forum Java TipReplies: 0Last Post: 11-15-2007, 03:18 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks