user2640722
user2640722

Reputation: 113

How to instantiate an object in java?

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);
    }
}

Upvotes: 11

Views: 114354

Answers (7)

Daniel Moura
Daniel Moura

Reputation: 21

Well , it's easy. To you instantiate an object in Java you should to use class name and to do with that it class received a valor. For exemplo :

...Car c1 = new Car();

Upvotes: 0

MuchMore
MuchMore

Reputation: 168

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.

Upvotes: 4

abhinav
abhinav

Reputation: 1282

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));
}

Upvotes: 1

AllTooSir
AllTooSir

Reputation: 49362

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);
 }
}

Upvotes: 18

A.Mokhtari
A.Mokhtari

Reputation: 461

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);
}

Upvotes: 1

Vadim
Vadim

Reputation: 2865

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);

Upvotes: 1

Suresh Atta
Suresh Atta

Reputation: 121998

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 );

Upvotes: 1

Related Questions