How to instantiate an object in java?

后端 未结 7 1023
不思量自难忘°
不思量自难忘° 2020-12-01 13:14

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 Sa         


        
相关标签:
7条回答
  • 2020-12-01 13:37

    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));
    }
    
    0 讨论(0)
  • 2020-12-01 13:41

    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);
    }
    
    0 讨论(0)
  • 2020-12-01 13:48

    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);
     }
    }
    
    0 讨论(0)
  • 2020-12-01 13:50

    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 );
    
    0 讨论(0)
  • 2020-12-01 13:52

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

    0 讨论(0)
  • 2020-12-01 13:53

    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.

    0 讨论(0)
提交回复
热议问题