# Question on java (the programming language)

**URL:** <https://boards.straightdope.com/t/question-on-java-the-programming-language/666990>\
**Category:** Factual Questions\
**Created:** [August 23, 2013, 7:11pm UTC](https://boards.straightdope.com/t/question-on-java-the-programming-language/666990 "2013-08-23T19:11:54Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Saint\_Cad](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/saint_cad/32/18907_2.png) [@Saint\_Cad](https://boards.straightdope.com/u/Saint_Cad)\
**Post date:** [August 23, 2013, 7:11pm UTC](https://boards.straightdope.com/t/question-on-java-the-programming-language/666990/1 "2013-08-23T19:11:54Z")

</div>

Teaching myself java after having programmed in pascal and ANSI-C. I’m starting to play around with writing methods and I wrote this:

> [@](#):
>
> public class firstmethod {  
> int summate (int add1, int add2, int add3) {  
> int total = add1 + add2 + add3;  
> return total;  
> }  
> public static void main (String args) {  
> int first = 1;  
> int second = 3;  
> int third = 9;  
> int answer = summate (first, second, third);  
> System.out.println ("The answer is " + answer + “.”);  
> }  
> }

Of course got the infamous “non-static method cannot be be referenced from a static context.” After trying to understand the error, I changed the second line to:

> [@](#):
>
> static int summate (int add1, int add2, int add3) {

1. What exactly does that error mean?
2. How did making _summate_ static solve the problem?
3. Is there a better way to do this?

---

<div class="post-metadata">

**Author:** ![yellowjacketcoder](https://avatars.discourse-cdn.com/v4/letter/y/4bbf92/32.png) [@yellowjacketcoder](https://boards.straightdope.com/u/yellowjacketcoder)\
**Post date:** [August 23, 2013, 7:20pm UTC](https://boards.straightdope.com/t/question-on-java-the-programming-language/666990/2 "2013-08-23T19:20:22Z")

</div>

1. Static methods/variables are members of the class, while instance methods/variables are members of an _instance_ of the class. Since you aren’t creating an instance of “firstmethod”, you can’t call any method on that instance.

2. Making the method static means you can call the method without instantiating the class first.

3. Create an instance of firstmethod with a constructor as shown: firstmethod m = new firstmethod();

4. Incidentally, it’s standard Java naming to always name a class starting with a capital letter, always name a variable starting with a lowercase letter, and use camelCase everywhere except constants. I can’t tell you how unconfortable it was for me to not capitalize the name of your class 🙂

---

<div class="post-metadata">

**Author:** ![Terr](https://avatars.discourse-cdn.com/v4/letter/t/839c29/32.png) [@Terr](https://boards.straightdope.com/u/Terr)\
**Post date:** [August 23, 2013, 7:23pm UTC](https://boards.straightdope.com/t/question-on-java-the-programming-language/666990/3 "2013-08-23T19:23:34Z")

</div>

You have to understand what “static” means. A class is a collection of variables/properties and methods/functions. It is an abstract thing - a “concept” if you will. In order to use the class, you have to “instantiate” it - that is, create an instance of the class, make a “physical” model of the class rather than abstract. After you do that, you can reference the variables and call the methods of the class. When the class is not instantiated, there’s nothing to call - those properties and methods exist only as abstract “concepts”. It’s like blueprints vs. a building - the door may exist in the blueprint as a concept, but it does not exist until you “instantiate” the blueprint as a building and you cannot open the door in the blueprint - only in the building.

Except for “static” properties or methods. Those exist independently of the instances of the class and apply to the whole class rather than to the instances. For example, you may have blueprints (“class”) and buildings built from the blueprints (“instances”). But if you want to know the total number of buildings ever built from the blueprints, you may have a static property called “TotalBuildings” that is static - it exists in the class, not in any particular instance. The property can be referred to from any instance of the class, or from outside the class, it is the same for all those calls. Static properties or methods do not need any instance of the class to exist in order to be called, they are called by referring to the class, not to the instance.

Now we get to the error. Since static functions exist in the class, but not in any particular instance, they cannot reference any instance properties or methods - since those only exist when instances exist, and the static function may be called before any instance exists.

---

<div class="post-metadata">

**Author:** ![leahcim](https://avatars.discourse-cdn.com/v4/letter/l/b4bc9f/32.png) [@leahcim](https://boards.straightdope.com/u/leahcim)\
**Post date:** [August 23, 2013, 7:27pm UTC](https://boards.straightdope.com/t/question-on-java-the-programming-language/666990/4 "2013-08-23T19:27:55Z")

</div>

Static methods are methods defined on the _class_ – there is no instance of the class associated with this method. Non-static methods are associated with _instances_ of the class – there is required to be an instance of the class for a call to one of those methods to make sense.

Non-static methods can call both static and non-static methods. If calling a non-static method, the associated instance is the same as the instance associated with this method. But static methods can only call other static methods because there is no instance already associated with them.

Out of necessity main() is always a static method. At the start of the program, no classes have been instantiated, so only a static method could be called. But an alternate way to correct your program would be to have main() instantiate your class before calling the method, like:

```auto

public static void main (String[] args) {
int first = 1;
int second = 3;
int third = 9;
firstmethod x = new firstmethod();
int answer = x.summate (first, second, third);
System.out.println ("The answer is " + answer + ".");
}

```

That way summate is being called on a specific instance (i.e. x) of the class, rather than in a static context.

If you are not entirely comfortable with Object-Oriented programming, you should try to become more comfortable with it in order to use the language, since in Java OO is obligatory.

---

<div class="post-metadata">

**Author:** ![Saint\_Cad](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/saint_cad/32/18907_2.png) [@Saint\_Cad](https://boards.straightdope.com/u/Saint_Cad)\
**Post date:** [August 23, 2013, 8:01pm UTC](https://boards.straightdope.com/t/question-on-java-the-programming-language/666990/5 "2013-08-23T20:01:54Z")

</div>

I think I got Terr’s analogy but let me just check my understanding.

I want to make a method turndoorknob. As long as it is not instanced viz just blueprints with no real door and I try to call turndoorknob, the compiler says “You can’t because you can’t turn the theoretical (blueprint) doorknob. You need a real doorknob to turn so make a door first.”

So now I write  
Door frenchdoor = new Door();  
and now java will let me call frenchdoor.turndoorknob since there’s actually a door/knob to turn.

Please say I got it because using that analogy to see if I understood, I made my first object complete with methods that can be called from another class. And it WORKS!

---

<div class="post-metadata">

**Author:** ![Kinthalis](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/kinthalis/32/16084_2.png) [@Kinthalis](https://boards.straightdope.com/u/Kinthalis)\
**Post date:** [August 23, 2013, 8:04pm UTC](https://boards.straightdope.com/t/question-on-java-the-programming-language/666990/6 "2013-08-23T20:04:22Z")

</div>

100% correct.

---

<div class="post-metadata">

**Author:** ![Xema](https://avatars.discourse-cdn.com/v4/letter/x/9de053/32.png) [@Xema](https://boards.straightdope.com/u/Xema)\
**Post date:** [August 24, 2013, 2:13am UTC](https://boards.straightdope.com/t/question-on-java-the-programming-language/666990/7 "2013-08-24T02:13:47Z")

</div>

> [@yellowjacketcoder](#):
>
> I can’t tell you how unconfortable it was for me to not capitalize the name of your class

Might also be worth noting that it’s probably not a great idea to use “method” as part of the name of a class.

---

<div class="post-metadata">

**Author:** ![Senegoid](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/senegoid/32/6606_2.png) [@Senegoid](https://boards.straightdope.com/u/Senegoid)\
**Post date:** [August 24, 2013, 2:45am UTC](https://boards.straightdope.com/t/question-on-java-the-programming-language/666990/8 "2013-08-24T02:45:34Z")

</div>

The responses above are right. _Maybe_ I can offer a bit of additional explanation:

If a class MyClass contains a non-static variable myVar (also known as a field or property), then _each_ instance of a MyClass contains its _own_ instance of the myVar variable. (In other, more rational universes, this would be called an _instance_ variable. It seems quirky to me, to use the word “static” to define a class variable or method. Seems like a word borrowed from C and given a different meaning.)

Now, let:  
MyClass period1 = new MyClass() ;  
MyClass period2 = new MyClass() ;

Now, call a method within the class, using each of these two instance objects:

int totalCredits = period1.hours() + period2.hours() ;

The method hours() _might_ (and typically would) contain references to instance variables within the class. The two calls to hours() above would each refer to its own instance’s instance of myVar. So how does the code in hours() know which instance is this in each call?

Because, the target object is passed to the method as a (poorly kept secret) parameter. In the call period1.hours(), the object period1 is one of the parameters parameters passed to the function (even it isn’t called a parameter in OO terminology). In a non-OO language, the (near)-equivalent call would be:

int totalCredits = hours(period1) + hours(period2) ;

Now to the point: Instance (non-static) methods, by definition, are intended to be used in this way: To include references to instance variables in each particular this object when they are called. Thus, there absolutely _must_ be a this when the method is called.

The example in the OP might be confusing because it’s a degenerate case: The body of the function does _not_ refer to any instance variable of the class. Therefore, it hypothetically could be called with:

int answer = summate (first, second, third);

Except that it’s _defined_ as an instance (non-static) method. Thus, even though it doesn’t refer to any instance variables, it _could._ Thus, it must be called with a this object, as in:

int answer = myThisObject.summate (first, second, third);

so that any references to instance variables will have, you know, an instance to work with.

Thus, if a method is intended to _not_ refer to variables of any particular instance, but instead is intended to be used as in the OP example, then the method must be declared as a static (i.e., class) method rather than an instance method.

---

<div class="post-metadata">

**Author:** ![ultrafilter](https://avatars.discourse-cdn.com/v4/letter/u/3d9bf3/32.png) [@ultrafilter](https://boards.straightdope.com/u/ultrafilter)\
**Post date:** [August 24, 2013, 4:05am UTC](https://boards.straightdope.com/t/question-on-java-the-programming-language/666990/9 "2013-08-24T04:05:24Z")

</div>

Use [noparse]

```auto

```

[/noparse] instead of [noparse]

> [@](#):
>

[/noparse] for code blocks to preserve the indentation. It makes your code much easier for us to read.

---

<div class="post-metadata">

**Author:** ![Bremidon](https://avatars.discourse-cdn.com/v4/letter/b/ac91a4/32.png) [@Bremidon](https://boards.straightdope.com/u/Bremidon)\
**Post date:** [August 24, 2013, 8:33am UTC](https://boards.straightdope.com/t/question-on-java-the-programming-language/666990/10 "2013-08-24T08:33:40Z")

</div>

Just as an aside, after looking at the languages you originally used to learn programming, it might help you to pick up a book (or scrounge the Internet for articles) about the basics of Object Oriented Programming (or OOP for short).

You would probably understand Java better if you picked up the basic reasons why OOP was invented in the first place, because Java strives with some success to be a model OOP language. It will also help you if you ever decide to pick up C++, C#, or any one of the other gazillion OOP languages out there.

---

<div class="post-metadata">

**Author:** ![kferr](https://avatars.discourse-cdn.com/v4/letter/k/71e660/32.png) [@kferr](https://boards.straightdope.com/u/kferr)\
**Post date:** [August 24, 2013, 12:45pm UTC](https://boards.straightdope.com/t/question-on-java-the-programming-language/666990/11 "2013-08-24T12:45:55Z")

</div>

I agree 100% with **Bremidon**. I started life as a Fortran and Cobol programmer and picked up C easily enough, but didn’t see what C++ was trying to achieve. After my company sent me on an OOP design course it all made sense and picking up Java and C# was easy. I still don’t like C++, I think it’s ugly.

---

<div class="post-metadata">

**Author:** ![Saint\_Cad](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/saint_cad/32/18907_2.png) [@Saint\_Cad](https://boards.straightdope.com/u/Saint_Cad)\
**Post date:** [August 24, 2013, 2:45pm UTC](https://boards.straightdope.com/t/question-on-java-the-programming-language/666990/12 "2013-08-24T14:45:19Z")

</div>

I was considering buying [this book](http://www.amazon.com/gp/product/0321498054/ref=as_li_qf_sp_asin_il_tl?ie=UTF8&tag=introcs-20&linkCode=as2&camp=1789&creative=9325&creativeASIN=0321498054) but I was hesitant to buy it blind. Does anyone have an opinion on it?
