I doubt you actually want to create an object.
From your code snippet, I understand that you want to run a 'method' named Sample
which adds two numbers. And in JAVA you don't have to instantiate methods. Objects are instances of class
. A method is just a behavior which this class has.
For your requirement, you don't need to explicitly instantiate anything as when you run the compiled code JAVA automatically creates an instance of your class and looks for main()
method in it to execute.
Probably you want to just do following:
public class Testing{
private int sample(int a, int b) {
return a + b;
}
public static void main(String[] args) {
int c = sample(1, 2);
System.out.println(c);
}
}
Note: I changed Sample
to sample
as it's generally accepted practice to start a method name with lower-case and class name with an upper-case letter, so Testing
is correct on that front.