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=
Printable View
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=
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.
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.
thank for you help
Is the same methods of different objects occupy seperate memory too?
You cannot have objects for methods. Because you cannot implement methods individually in Java. You had to have a container like class.
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").
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.
Code:long round = Math.round(.999); //round is a static method of class Math
Thank you.
I understand now!
And read more on Suns' tutorial. Well explained all those concepts.
Lesson: Classes and Objects (The Java™ Tutorials > Learning the Java Language)