Reputation: 73
I'm trying to call a method in another method (like in C#), like this:
public class Exercise1
{
Scanner scanner = new Scanner(System.in);
public int FirstNumber;
public int SecondNumber;
public int Answer;
public static void main(String [] args)
{
GetNumbers();
}
private void GetNumbers()
{
System.out.print("Type in the first number: ");
FirstNumber = scanner.nextInt();
System.out.print("Type in the second number: ");
SecondNumber = scanner.nextInt();
Answer = FirstNumber + SecondNumber;
System.out.print("The answer is: " + Answer);
}
}
Why can't I call the method like that?
Upvotes: 2
Views: 7994
Reputation: 13854
main is static method whereas GetNumbers()
is not static
either make GetNumbers()
to static or create an object in main then call GetNumbers()
like this way
public class Exercise1
{
Scanner scanner = new Scanner(System.in);
public int FirstNumber;
public int SecondNumber;
public int Answer;
public static void main(String [] args)
{
Exercise1 e=new Exercise1();
e.GetNumbers();
}
private void GetNumbers()
{
System.out.print("Type in the first number: ");
FirstNumber = scanner.nextInt();
System.out.print("Type in the second number: ");
SecondNumber = scanner.nextInt();
Answer = FirstNumber + SecondNumber;
System.out.print("The answer is: " + Answer);
}
}
or
public class Exercise1
{
static Scanner scanner = new Scanner(System.in);
public static int FirstNumber;
public static int SecondNumber;
public static int Answer;
public static void main(String [] args)
{
GetNumbers();
}
private static void GetNumbers()
{
System.out.print("Type in the first number: ");
FirstNumber = scanner.nextInt();
System.out.print("Type in the second number: ");
SecondNumber = scanner.nextInt();
Answer = FirstNumber + SecondNumber;
System.out.print("The answer is: " + Answer);
}
}
Upvotes: 2
Reputation: 122026
You cannot access non static
methods in a static
context.
Since main method is static you cannot access non static
methods inside it.
Possible solutions:
Solution 1.
Make your GetNumbers();
as static
. Then you are able to access it.
private static void GetNumbers()
{
}
But, I won't recommend in your case, because you are accessing other instnace mebers too in GetNumbers()
method. So they also needs to be static
.
Solution 2.
Create new object for Exercise1
class inside main method.
public static void main(String [] args)
{
Exercise1 ex= new Exercise1();
ex.GetNumbers();
}
private void GetNumbers()
{
System.out.print("Type in the first number: ");
FirstNumber = scanner.nextInt();
System.out.print("Type in the second number: ");
SecondNumber = scanner.nextInt();
Answer = FirstNumber + SecondNumber;
System.out.print("The answer is: " + Answer);
}
And as a side note:
Please follow java naming conventions, variable names stats with small letter's.
public int firstNumber;
public int secondNumber;
public int answer;
Upvotes: 9
Reputation: 15428
GetNumbers();
needs to be static
. A static method can only call a static method
or use static fields
.
However, but it can access object.GetNumber()
if the object
instance of Exercise1
is local to the main()
. That is, create an instance object of Exercise1
in main()
method to access that Exercise1
object's GetNumber()
method.
Upvotes: 4