I have a question which is how can i justify which is static and which is non static method in a program? I need to explain why ? I think it the version 2 which is static. I can only said that it using using a reference. But am I correct or ?? Please advise.... if I have the following : ... |
If it's static, there's one ref variable that's associated with the class as a whole. If you change it in any way every instance will see it. If i's non-static, then each instance has its own copy, and changing one will not change the other, BUT, in this case, since it's an instance of that class itself, every time you create ... |
A non-static method belongs to an instance of a class and not to the class itself. That is it belongs to an actual object. So lets say you create 100 objects and you call a non-static method like start, which one of those 100 objects' start method should be invoked? You need to specify which objects method you want to call. ... |
|
|
|
Can someone please explain the advantages in implementing one or the other? I know static methods are available for the object in general and non static methods for the instance of the object. I'd like to know instead when is better to use non static methods. Also, I read somewhere non static methods are not recommended when using inheritance, why? Thanks ... |
|
Seems pretty clear to me: "Why instantiate a class when you can make the methods static?" Yes thats the write question, Sorry I didn't asked it properly. Because just writing static methods violates the whole point of using object-oriented programming, which is encapsulation. Take a step back and look at computer programming, or better yet the evolution of computer programming, as ... |
The problem is probably that he is calling a non-static method ( isDelivered() ) from within a static method ( probably main() ) Either change the declaration of isDelivered() to static, or instantiate an instance of you class and call isDelivered() on that instance. I'm not sure, but I think you are wrong. The OP says that he is using jSMS, ... |
Non-static methods are associated with an instance of the class. They typically operate on state (member variables) that's specific to on object. They represent the behavior of that one object. Static methods are associated with the class as a whole. There's no implie "current" object, so they're used for operations that are not part of the behavior of an individual object ... |
private TestObject(int ID) { System.out.println("TestObject is been created ID = " + ID); } public (static) TestObject getTestObject(int ID) { if (ref == null) ref = new TestObject(ID); return ref; } } public class SingletonJavaExample { int IntValue = 8; (static) TestObject PartOfSO1, PartOfSO2; String DataString = new String("SingletonObject"); public static void main(String args[]) { SingletonJavaExample SO1 = new SingletonJavaExample(); PartOfSO1 ... |
|
|
|
|
Ok ill try and explain better: If i had "jtp.insertIcon(icon);" in the "public static ImageIcon gameState(int count) " method it would not work as jtp is a JTextPane and cannot be used in a static method. What I am trying to do is passed the value of icon from the static method (the middle one) to the bottom method so that ... |
|
My problem with the underlined is that, while technically true, I can see where a newbie would take it as "oh, so that's how I can get rid of static," and focus only on how to satisfy the compiler, rather than on learning a) when it's appropriate to make something static or not from a design perspective, and b) what the ... |
int f() throws IllegalArgumentException, SecurityException, IllegalAccessException, NoSuchFieldException { return d(this,this); } int d(A a1, A a2) throws IllegalArgumentException, SecurityException, IllegalAccessException, NoSuchFieldException { int i = a1.x; g(a1); int j = a2.x; return j - i; } static void g(A a) throws IllegalArgumentException, SecurityException, IllegalAccessException, NoSuchFieldException { // uses reflection to change a.x to 2 |