8 Replies - 305 Views - Last Post: 23 July 2019 - 08:28 AM Rate Topic: -----

#1 GingerSnacks   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 23-July 19

Static Method Issue

Posted 23 July 2019 - 06:12 AM

Hi all,

Got one error from the printstack and it has something to do with the main method line not sure exactly.


public class Playing
{
	boolean playButton; // true or false

public static void main(String [] args)
{
 play();
}

public void play()
{
    if(playButton)
    {
        System.out.println("Music is playing");
    }
    else
    {
        System.out.println("Music is paused");
    }
}
}






Is This A Good Question/Topic? 0
  • +

Replies To: Static Method Issue

#2 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 780
  • View blog
  • Posts: 5,854
  • Joined: 25-December 13

Re: Static Method Issue

Posted 23 July 2019 - 06:43 AM

Quote

error from the printstack

Please copy the full text of the error message and paste it here so we can see what happened.
Was This Post Helpful? 1
  • +
  • -

#3 GingerSnacks   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 23-July 19

Re: Static Method Issue

Posted 23 July 2019 - 07:01 AM

Playing.java:7: error: non-static method play() cannot be referenced from a static context
play();
^
1 error
Was This Post Helpful? 0
  • +
  • -

#4 xclite   User is offline

  • I wrote you an code
  • member icon


Reputation: 1389
  • View blog
  • Posts: 4,220
  • Joined: 12-May 09

Re: Static Method Issue

Posted 23 July 2019 - 07:07 AM

The play method isn't static (this is what it means when it says "non-static method", right?).

You can't call non-static things from static things because non-static things are attached to an instance of an object.

You could call play like:

Playing playing = new Playing();
playing.play();



If you aren't doing objects yet, and just want to call methods from main, you need to make the play method static, like your main is.
Was This Post Helpful? 0
  • +
  • -

#5 GingerSnacks   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 23-July 19

Re: Static Method Issue

Posted 23 July 2019 - 07:28 AM

Hi I tried the suggestion above but got the same error. I've did static play inside the main method and also got another error
Was This Post Helpful? 0
  • +
  • -

#6 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 780
  • View blog
  • Posts: 5,854
  • Joined: 25-December 13

Re: Static Method Issue

Posted 23 July 2019 - 07:38 AM

Quote

got another error

Please copy the full text of the error message and paste it here so we can see what happened.
Was This Post Helpful? 1
  • +
  • -

#7 GingerSnacks   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 23-July 19

Re: Static Method Issue

Posted 23 July 2019 - 07:41 AM

Got it to work, the other weird printstack messages disappeared.

Though i'm interested how it it work as a static method when as adding static to where play is in the main method line


Playing.java:7: error: illegal start of expression
static play();
^
Playing.java:7: error: invalid method declaration; return type required
static play();
^
Playing.java:10: error: class, interface, or enum expected
public void play()
^
Playing.java:15: error: class, interface, or enum expected
}
^
Playing.java:19: error: class, interface, or enum expected
}
^
5 errors
Was This Post Helpful? 0
  • +
  • -

#8 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 780
  • View blog
  • Posts: 5,854
  • Joined: 25-December 13

Re: Static Method Issue

Posted 23 July 2019 - 07:51 AM

static is used when declaring a method,
it is not used when calling a method.
Was This Post Helpful? 1
  • +
  • -

#9 xclite   User is offline

  • I wrote you an code
  • member icon


Reputation: 1389
  • View blog
  • Posts: 4,220
  • Joined: 12-May 09

Re: Static Method Issue

Posted 23 July 2019 - 08:28 AM

Yup. Also, instead of just guessing at this stuff, I'd recommend taking a step back. I suggested that the main method was already static. If you look at your main method, the static keyword is in front of the place where main is declared and defined. Putting static in front of a call to play wouldn't really follow from that... why wouldn't you try the same approach and put static in the same place for play as it is for main?

I say this not to harp (well, maybe it is to harp), but to let you know that coding will always be like this - just throwing things at the wall isn't how you get good at this. Understanding how to use static and where it goes is a far better strategy, and even more generally identifying patterns and concepts is key to become a skilled programmer.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1