• Post Reply Bookmark Topic Watch Topic
  • New Topic

non-static method  RSS feed

 
Urs Waefler
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the code: It does not compile because the method sM1 is not static. The variables s1 and s2 are static. I do not understand this concept.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
since sM1 is not static, you can't call it unless you have a object of that type.

What you are trying to do right now is like trying to read a dog's name off it's tag, but there isn't a dog (and thus, no tag).
 
Urs Waefler
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I begin to understand; but I do not understand it fully yet. The example with the dog is good, but let us explore this code in more details. The variable s1 is static. There is an assignment of a non-static method. I do not see where the problem exactly arises.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The method sM1 can only be run when you have created an object of type InitTest, because it is non-static.

Generally, non-static methods need to know about or change something in the objects state. So take a bank account.  You can't say "give me the current balance" or "increase the balance by <deposit_amount>"  unless there is an actual bank account.

For methods that don't require any knowlege of an object's state, you can make them static. The Math class is the classic example of this. You don't need to create a Math object to calcualte the sine of an angle, so all its methods are defined as static. You can simply pass in the angle measurment, and it computes a floating point value to return (not sure if it's an actual Float or a Double).
 
salvin francis
Bartender
Posts: 1664
37
Eclipse IDE Google Web Toolkit Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Urs,
As I see from your post, you probably need to understand static a bit more before understanding your code. I must say that the variable names and method names are quite bad and hence they may be confusing for you.
You may want to look at https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html ; It explains static in detail.

Here's a simple code:


output:
 
salvin francis
Bartender
Posts: 1664
37
Eclipse IDE Google Web Toolkit Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now, lets get teleported in a fictional world where java did allow a static variable to access an instance method as per your understanding ...

Note that this program does not compile and will not work, but I am only writing it for demonstration purposes:



What should have been the output for the above code according to you ?
 
Urs Waefler
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand the concept very well now. Nevertheless the code I posted seems to be not that easy to understand; your example is easy. I am stuck at this point: If I remove static this line does not cause a problem. I wonder how the compiler "thinks". I think, first it sees: What happens after? = has the lowest priority. Thus I think it looks at sM1("a") after, it sees that sM1(String s) is a non-static method. In what way are my thoughts correct until here?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think i see where part of the confusion is. Forget about your strings.  In fact, let's simplify your code:



This compiles. It does nothing, but it compiles.  but if I do this, it won't:



It's not because of any String variable I have declared. It's because I'm trying to call a non-static method (sM1) from a static contect - the body of the main method.  If I create an instance of sM1, I can call the method off my reference:



Now I'm calling the method sM1 from a non-static context - i have an actual instance.  What I do with the string it returns doesn't matter.
 
Urs Waefler
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It has to do with the context only. private String sM1(String s) is a non-static method. I am calling the method private String sM1(String s) from a static context. Is this correct?
 
salvin francis
Bartender
Posts: 1664
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Urs Waefler wrote:private String sM1(String s) is a non-static method.

This is correct

Urs Waefler wrote: I am calling the method private String sM1(String s) from a static context. Is this correct?

This is also correct

So, now that you know how it works, did you try making the method "sM1" static ?
 
Urs Waefler
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Initially it was static.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know I am late to the fight, but I seem to have had a busy weekend.
It worries me when I see people trying to swap things between static and non&#s2011;static. Does that betoken a misunderstanding of what static means? I also worry about people saying non‑static. Do people say that because there is a compiler error message from the Sun/Oracle javac tool which says non‑static? If only people would use the correct term: instance method. Or maybe we shou‍ld call them object methods or normal methods? Maybe we shou‍ld call static methods abnormal methods I know I started off learning Java® without an understanding of what static means, but that means I was taught to think about objects, which is how is shou‍ld be in an OO language.
 
Shibu Kurian
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me put it this way...

Few facts about instance variables:
1. They can be directly accessed from within instance(non-static) methods. //As you can see in m1() method
2. As and when different objects are created, a dedicated copy of the instance variables also gets created. // Line 1 and 3
i.e the application while running might contain different copies of the same variable name(x).
Now, Since there are many copies of instance variables at a time, the only way the JVM can determine which copy of x are we referring to in m1 method is by the calling method. //Lines 2 and 4

Facts about static variable:
3. Only one copy of the variable will be present at a time irrespective of the number of objects getting created. So it can be directly accessed from instance or static methods.
4. static modifier when used along with a method means any user can access this method. Rather than saying object is not required, I would say object is not mandatory to call such methods.

In your code, you're trying to call a instance method sM1 from a static method(main).

s1 = sM1("b"); //Line 5

Now assume if compilation was successful for Line 5 of your code, if there were any instance variables used in the instance method sM1, the JVM will never come to know which copy of the instance variable are you referring to(explained in point# 2). So, java never allows any scenarios of an instance method being called from a static context and hence compilation fails as you've not followed the JVM standards in your coding.

It does not compile because the method sM1 is not static.
--> Incorrect
It does not compile because of the call to the method .

//static String s1 = sM1("a");
Here sM1("a") is called from a static context whereas
//String s1 = sM1("a");
Its not called from a static context and hence no error.
Same case for s2.

Hope this gave you some light on to how to program. FYI, General real time usage of static methods are functionalities that are provided to the public like loan calculator you see in bank websites. The functionality can be accessed by anyone using the website. If you've noticed, it does not contain any customer specific information --> technically means no instance variables.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!