aspose file tools*
The moose likes Beginning Java and the fly likes Beginner Mistake I bet you get this 100 times a year Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Beginner Mistake I bet you get this 100 times a year" Watch "Beginner Mistake I bet you get this 100 times a year" New topic
Author

Beginner Mistake I bet you get this 100 times a year

Mike Osterhout
Ranch Hand

Joined: Jun 29, 2009
Posts: 84
I have two files and I am trying to call a method which is part of a private class. I get the following error: non-static method calculate(int,int) cannot be referenced from a static context.

Any help is greatly appreciated on this.


This is the file calling the method.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3947
    
  17

Some questions for you:

1) What is a static method?
2) How do you call a static method?
3) Conversely then, what is a non-static method?
4) What do you need in order to call a non-static method that you don't need in order to call a static method?
5) How do you get one?


Steve
Mike Osterhout
Ranch Hand

Joined: Jun 29, 2009
Posts: 84
1) What is a static method?
A static method can be called on both objects and instances in the class, and it belongs to the class, and all instances of the class.

2) How do you call a static method?
That was the question of the post, I am not sure.

3) Conversely then, what is a non-static method?
A method that can only be called on instances in the class.

4) What do you need in order to call a non-static method that you don't need in order to call a static method?
That was the question of the post, I am not sure.

5) How do you get one?
How do I get what?


It is obvious I am lost. I spent a good amount of time researching this stuff but I was not able to get the answer to this. I hope you can shed some light on this, by turning the light bulb on in my head. I am certain, by this point, I wont forget this concept once I get the answer.

Thanks.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Here's the offending line:

double total = CoffeeCalculator.calculate(weight, sold);

This (NameOfClass.nameOfMethod()) is how you call a static method. It only works for static methods.

To call non-static methods like nextInt() in java.util.Scanner, or println() in java.io.PrintStream, you need an object of that class. You can't call PrintStream.println(); you need a PrintStream object (that's what "System.out" is). YOu can't call Scanner.nextInt(); you need to actually create a Scanner object and use that. You've done that: you created an instance of Scanner and stored it in the variable "scanner".

So that's what you need to do with calculate(): create an instance of CoffeeCalculator (ie., using "new CoffeeCalculator()") and then call the method on that instance.


[Jess in Action][AskingGoodQuestions]
Mike Osterhout
Ranch Hand

Joined: Jun 29, 2009
Posts: 84
That is the problem. I am required to keep the constructor for the CoffeeCalculator class set to private. That makes it impossible to create an instance of the CoffeeCalculator class and thus in my mind, very hard to use the calculate method.

Are you saying to do it the way I am trying to do it, I can't use scanner?

Thanks for the help.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Mike Osterhout wrote: I am required to keep the constructor for the CoffeeCalculator class set to private. That makes it impossible to create an instance of the CoffeeCalculator class and thus in my mind, very hard to use the calculate method.


If you can't bring Mohammad to the mountain, bring the mountain to Mohammad. Or, in less cryptic terms, if you can't create an instance of CoffeeCalculator so that you can call a non-static version of calculate(), then make calculate static, and call it the way you're calling it now.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2969
    
    9
There are other options as well. If your instructor has made any mention of factory methods or the singleton pattern, those are routes you might well follow here, allowing you to use a static method to obtain an instance of CoffeeCalculator. This might be what your instructor is trying to get you to do. On the other hand, if neither "factory method" nor "singleton" has been mentioned yet, you should probably just ignore this post, and stick with what EFH said.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 9990
    
    7

Hi Mike Osterhout,
In future, while posting code, please do use code tags. I have edited your existing post, and as you can see, it is much more readable and easier to understand.

Edit: Didn't realize there were two Mikes in this post


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
Notice that the price calculation method doesn't take any information specific to an instance. Nor does it alter any information specific to an instance. It takes price which is the same for all instances (and should therefore be static) and tax which is the same etc.

Any method which doesn't use any instance-specific information and doesn't alter any instance-specific information is a candidate for being declared as static.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
The reason you were told to make the constructor private is so as to make the calculator into a "utility class." Look at the Math class, which is a utility class with a private constructor (so you don't see the constructor in the documentation). It only has static methods; you pass a number and it works out the log or sine or whatever. There is no record of the number you pass in the Math class anywhere.

Similarly in your coffee calculating class, there is no need to remember whether you sold ½ pound of coffee or ¼ pound. It simply works out the cost.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
If you want to print out PRICE_PER_POUND and you have made it static, precede PRICE_PER_POUND by the name of its class.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3947
    
  17

Mike Osterhout wrote:1) What is a static method?
A static method can be called on both objects and instances in the class, and it belongs to the class, and all instances of the class.


I would change that a bit (because 'A static method can be called on both objects and instances in the class' is confusing. Maybe say:
A static method belongs to the class, and is shared by all instances. A static method is called from the class name itself. Static methods may be called from instances of the class but this is confusing, usually, and should be avoided.


2) How do you call a static method?
That was the question of the post, I am not sure.

I thought that was the easy part - that is what you are doing in your example. Say you have a class like this:

You call the method from the class itself:




3) Conversely then, what is a non-static method?
A method that can only be called on instances in the class.

4) What do you need in order to call a non-static method that you don't need in order to call a static method?
That was the question of the post, I am not sure.


Think about it. A static method needs the class. A non-static method needs to be called on an instance. So what does the non-static method need? Hint, You mention it in answer to 3, and I mention it in this reply...


5) How do you get one?
How do I get what?


Once you get the answer to question 4, then you can try to answer this.

I know this has been answered a few times already, but I just figure I would prompt you to think a little bit more. It has also been suggested that you can make the calculate method static because it doesn't use any instance dependent data. You can make calculate static, but it does depend on instance values. How else must you change your CoffeeCalculate class if you want to make calculate static.

As an alternative, you can keep calculate and the other data instance members, but then you need to provide an <.... insert answer to number 4 here ;-) ....> to the caller. If the CoffeeCalculator constructor is private then you can only make a new CoffeeCalculator in the CoffeeCalculator class itself. Then you have to make it available to others - usually by a static method. Which is what the Singleton pattern, as already mentioned, is all about.
Mike Osterhout
Ranch Hand

Joined: Jun 29, 2009
Posts: 84
Wow thanks for all the great help.

I was able to call my method by making it public and static.

I am now wondering how I would go about using the two constants located at the top of the CoffeeCalculator class. It appears they dont abide by the same rules.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
Your constants ought to be declared as

public static final double PRICE_PER_POUND = 5.99;
public static final double TAX_RATE = 0.0825;
Mike Osterhout
Ranch Hand

Joined: Jun 29, 2009
Posts: 84
Yep, and I reread the thread and someone mentioned to precede the constant with the name of the class. That is exactly what I did, and bingo, I am calculating coffee how my processor wanted me to calculate coffee.

Thanks again for all the help. This forum looks like it could be a great resource on my journey to learning java.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
You're welcome

And thank you for what you said about this forum
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Beginner Mistake I bet you get this 100 times a year
 
Similar Threads
Scanners?! Where?!
Help with code
Adding nested loop in order to print out a table
help getting user supplied dates to display
New Objects keeping old values