instantiation - How to instantiate an object in java? -


i'm new in programming , know did go wrong in instantiating object. below code:

public class testing{     private int sample(int c)     {         int = 1;         int b = 2;         c = + b;         return c;     }     public static void main(string []args)     {         sample mytest = new sample();         system.out.println(c);     } } 

there no sample class in code . 1 have declared private method .

// private method takes int parameter , returns int private int sample(int c) {   int = 1;   int b = 2;   c = + b;   return c; } 

with current snippet , need instantiate testing class , make use of sample method. notice class definition preceded keyword class , in case class testing.

public class testing{   private int sample(int c)   {     int = 1;     int b = 2;     c = + b;     return c;  }   public static void main(string []args)  {     testing t = new testing(); // instantiate testing class object     int result = t.sample(1); // use instance t invoke method on     system.out.println(result);  } } 

but doesn't make sense, sample method returns 3 .

are trying :

class sample {  int a;  int b;   sample(int a, int b) {     this.a = a;     this.b = b;  }   public int sum() {     return + b;  } }  public class testing {  public static void main(string[] args) {     sample mytest = new sample(1, 2);     int sum = mytest.sum();     system.out.println(sum);  } } 

Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -