Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Beginner's nightmare  RSS feed

 
Sherry Jacob
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I created this program :
public class Exercise2
{
public static void main(String[] args)
{
double fahrenheit = 62.5;

/* Convert */
double celcius = f2c(fahrenheit);
System.out.println(fahrenheit + "F = " + celcius + "C");
}

double f2c(double fahr)
{
return (fahr - 32) * 5 / 9;
}

}

It does not compile stating that I cannot reference static method...
What does the error mean ?

Please help...
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sherry,

Welcome to JavaRanch!

Without telling you anything you don't want to know yet: because main() is marked "static" (which it must be), f2c() must also be:

static double f2c(double fahr) ...

Also, a bit of business: you may not have read our naming policy on the way in. It requires that you use a full, real (sounding) first and last name for your display name. A single name isn't enough. You can change your display name here. Thanks!
 
Daniel .J.Hyslop
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi sherry
if you were to declare your double fc2(double fahr) method with the static modifier then your code should work ie static double fc2(double fahr)
You are trying to call a static method (main method ) with non static method
 
Laurent Salse
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sherry,

Herebelow is the correct code :

Change "double f2c(double fahr)" into "static double f2c(double fahr)"
{
return (fahr - 32) * 5 / 9;
}


Explanation :
1.Since you call the method f2c with no reference to any object then this method is linked directly to the Class ... Therefore it must be declared 'static' ...

2. If the static method f2c were declared in another class (ex. Public class Converter(){ ...) then the call to the method in the main method would be written like that :
double celcius = Converter.f2c(fahrenheit);

since f2c is declared 'static' it can be used without instantiating the class Converter ...

Cool ?

Last advice : Get Eclipse as an IDE for your developement ... it is able to detect that kind of mistake automatically !
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't get Eclipse, or any other IDE for you development - yet.

there's too much to learn first. Such as things like this... Eclipse might tell you "This is wrong", but you'll never understand WHY. then when things go horribly awry (as they always do), you'll really be stuck.

use a simple text editor (i like editpad lite), and then come here and ask questions. ask more questions about the answers.

for now, just take the "Make f2c static", and compile and go. in a few days (weeks, hours, whatever), you may say to yourself "Boy, i wonder why i had to do that - then come back here and ask for more info.

If you're just starting to learn the language, having to learn an IDE is NOT what you need. it will hide some of the details of the language from you, and you NEED to know those details.

Eventually you will learn a way to run your method WITHOUT declaring it static, but that will come in time.

and we're always happy to help 'round here!!!
 
jin sun
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, off subject, but fred is right...before using an IDE, use a text editor because it's essential to learn the details that IDEs sometimes hide away from you. Also using a text editor will help ease the learning curve of learning an IDE.
 
Sherry Jacob
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Every1,
Thanx alot 4 all ur valuable comments and suggessions. Well, I'm using EditPlus as my java text editor. It's cool 4 a beginner I suppose.

I also got a thorough understanding of what mistake I made. I had defined the function in the class and not instantiated the class. So the function also must be defined as static coz it belongs to the class and not 2 any object of the class.

Thanx every1. I'll be back 4 more help.

Also, I have updated my name to depict my full name as told by 1 of the site administrators !!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!