Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Main method use Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Main method use" Watch "Main method use" New topic
Author

Main method use

Jim Sandman
Greenhorn

Joined: Jul 11, 2007
Posts: 17
I've got this Speaker class that implements the Adjustable interface (below). In the Speaker class, how do I code the Main method in order to print the string that's in the printStates method ?
---------------------------------------------------
public class Speaker implements Adjustable {

public static void main(String[] args){

}

boolean powerOn = true;
int vollevel;
int tonelevel;
Speaker speaker1 = new Speaker();

public void chgVolume(int vollevel){
vollevel = 8;
}
public void chgTone(int tonelevel){
tonelevel = 6;
}
public void printStates(){
System.out.println("Power On?"+ powerOn + "Volume:" + vollevel + " Tone:" + tonelevel);
}

}
-------------------------------------------------------
public interface Adjustable {
void chgVolume(int vollevel);
void chgTone(int tonelevel);
void printStates();

}
-------------------------------------------------------


J. Sandman<br />SCJA<br /> <br />"I love mankind...it's people I can't stand."<br /> Charlie Brown
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

You just call the printStates() method from the main(...) method.

But note, main(...) is static, and printStates() is not. So you'll have to have an instance of your Speaker class to call the printStates() method on.

Note that your class Speaker has a member variable of type Speaker:

This line is going to get you into trouble - it causes an infinite loop.

When you create a new Speaker object, the member variables of that new object are initialized. One of the member variables is a Speaker. So the member Speaker will be created. But it has in turn a Speaker as a member variable. That one will be initialized. And that last one again has a Speaker member variable. Etc...


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Jim Sandman
Greenhorn

Joined: Jul 11, 2007
Posts: 17
You just call the printStates() method from the main(...) method.
But note, main(...) is static, and printStates() is not. So you'll have to have an instance of your Speaker class to call the printStates() method on.


Yeah, I got an error earlier, regarding using non-static variables in a static method, when I tried to call the printStates() method from the main() method. You recommended I create an instance of the Speaker class to call the printStates() method on, but I thot that's what I was doing when I used the line:
Speaker speaker1 = new Speaker();

So, I should remove that line, right? Then what ?
Thanks for your assistance.
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
You need to put that line inside the main() method:



Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Jim Sandman
Greenhorn

Joined: Jul 11, 2007
Posts: 17
Success.
Thanks, guys!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Main method use
 
Similar Threads
Selection Does not contain a main type?
Interfaces + error message
Cannot find symbol error
Class in an interface
implementing interfaces