How to instantiate an object in java?

2019-01-14 13:33发布

问题:

I'm new in programming and I would like to know where did I go wrong in instantiating an object. Below is the code:

public class Testing{
    private int Sample(int c)
    {
        int a = 1;
        int b = 2;
        c = a + b;
        return c;
    }
    public static void main(String []args)
    {
        Sample myTest = new Sample();
        System.out.println(c);
    }
}

回答1:

There is no Sample class in your code . The one which you have declared is a private method .

// private method which takes an int as parameter and returns another int
private int Sample(int c)
{
  int a = 1;
  int b = 2;
  c = a + b;
  return c;
}

With the current snippet , You need to instantiate the Testing class and make use of the Sample method. Notice your class definition is preceded by the keyword class , in this case class Testing.

public class Testing{
  private int Sample(int c)
  {
    int a = 1;
    int b = 2;
    c = a + b;
    return c;
 }
  public static void main(String []args)
 {
    Testing t = new Testing(); // instantiate a Testing class object
    int result = t.Sample(1); // use the instance t to invoke a method on it
    System.out.println(result);
 }
}

But that doesn't really make sense, your Sample method always returns 3 .

Are you trying to do something like this :

class Sample {
 int a;
 int b;

 Sample(int a, int b) {
    this.a = a;
    this.b = b;
 }

 public int sum() {
    return a + b;
 }
}

public class Testing {
 public static void main(String[] args) {
    Sample myTest = new Sample(1, 2);
    int sum = myTest.sum();
    System.out.println(sum);
 }
}


回答2:

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.



回答3:

You instantiating correctly with new keyword ,But your calss name and method invoking is wrong

 Testing myTest = new Testing();
  int result =myTest.Sample(1);  //pass any integer value
  System.out.println(result );


回答4:

Sample is not a class, it is just a method. You cannot create instances of it. You only run it -

int sample = Sample(3);

If you wish for sample to be a class, define it as a class.

In your case, the method is not static is so you cannot directly access it from the Static method Main. Make it static so you could access it. Or just create a new instance of Testing and use it -

Testing testing = new Testing();
int sample = testing.Sample(3);


回答5:

Sample method returns integer, so get the result and use it anywhere.

public static void main(String []args)
{
    int myTest = Sample(4555);//input may be any int else
    System.out.println(myTest);
}


回答6:

This is how you should be doing this.

public class Testing{
public int Sample(int c)
{
    int a = 1;
    int b = 2;
    c = a + b;
    return c;
}
public static void main(String []args)
{
    // Creating an Instance of Testing Class
    Testing myTest = new Testing();
    int c =0;
    // Invoking the Sample() function of the Testing Class
    System.out.println(myTest.Sample(c));
}