• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Main method use

 
Jim Sandman
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();

}
-------------------------------------------------------
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15354
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Jim Sandman
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to put that line inside the main() method:

 
Jim Sandman
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Success.
Thanks, guys!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic