Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Create new instance

Options
  • 06-11-2009 3:29pm
    #1
    Closed Accounts Posts: 2,696 ✭✭✭


    hey people - code attached -
    1 class does simple addition sum and returns a value
    1 control for class that passes values

    Question - whats the syntax for creating a new instance of mySimpleAdd?
    and have a choice which instance to use from the control program?
     
    public class callSimpleAdd {
     int a = 5;
     int b = 5;
     int x;
     callSimpleAdd(){
      SimpleAdd mySimpleAdd = new SimpleAdd();
      System.out.println("Result is: " + mySimpleAdd.Addition(a, b));
     
     
     }
     
       public static void main (String[] args){
        new callSimpleAdd();
     
       }
    }
    
     
    public class SimpleAdd{
     int x;
     int y;
     int result;
     
     public int Addition(int x, int y){
      result = x + y;
      return result;
     }
     
    }
    


Comments

  • Registered Users Posts: 2,800 ✭✭✭voxpop


    public class callSimpleAdd {
     int a = 5;
     int b = 5;
     int x;
    
     
       public static void main (String[] args){
           SimpleAdd mySimpleAdd = new SimpleAdd();
           System.out.println("Result is: " + mySimpleAdd.Addition(a, b));
     
       }
    }
    


  • Closed Accounts Posts: 2,696 ✭✭✭mark renton


    you just copied that from what i have


  • Registered Users Posts: 2,082 ✭✭✭Tobias Greeshman


    john47832 wrote: »
    Question - whats the syntax for creating a new instance of mySimpleAdd?
    and have a choice which instance to use from the control program?

    Well mySimpleAdd is an instance variable, it's an instance of your class SimpleAdd. You've correctly created an instance of the class.


  • Closed Accounts Posts: 2,696 ✭✭✭mark renton


    I need 2 instances of the same class - do I need to create a new file or can I add code to this instance?


  • Registered Users Posts: 2,800 ✭✭✭voxpop


    class Maths {
    
    
       public int addInt(int a,int b){
          return a+b;
        }
    }
    
    class Runner {
    
    
    
      public static void main (String args[]){
         Maths m1 = new Maths();
         Maths m2 = new Maths();
    
         int result1 = m1.addInt(1,2); //=3
         int result2 = m2.addInt(2,3); //=5
    
     }
    }
    
    
    


  • Advertisement
  • Registered Users Posts: 1,916 ✭✭✭ronivek


    Well you simply use a constructor to create an instance of a specific class.

    So;
    SimpleAdd simpleAddA = new SimpleAdd();
    SimpleAdd simpleAddB = new SimpleAdd();
    ....
    

    I suggest you have a read over the Java tutorial; it might help make things clearer.


  • Closed Accounts Posts: 2,696 ✭✭✭mark renton


    Ok so I have about 150 lines of code for one instance ( a lot if cmd line interaction with user) - i'm thinking there has to be an easier way to create the second instance rather than copy n past the code again and rename instance - is there?


  • Registered Users Posts: 1,916 ✭✭✭ronivek


    john47832 wrote: »
    Ok so I have about 150 lines of code for one instance ( a lot if cmd line interaction with user) - i'm thinking there has to be an easier way to create the second instance rather than copy n past the code again and rename instance - is there?

    Simple answer is yes; there likely is a much easier way. Without seeing your code it's very difficult to know where you might be going astray however.

    In general if you want to perform the same series of steps multiple times you would extract those steps from your program and create a method with the required parameters.

    So for example instead of the following;
    SomeClass a = new SomeClass();
    a.valueX = console.ReadLine();
    a.valueY = console.ReadLine();
    
    SomeClass b = new SomeClass();
    b.valueX = console.ReadLine();
    b.valueY = console.ReadLine();
    
    You would extract the steps into a method;
    SomeClass instance1 = createSomeClassInstanceMethod();
    SomeClass instance2 = createSomeClassInstanceMethod();
    
    public SomeClass createSomeClassInstanceMethod() {
        SomeClass instance = new SomeClass();
        instance.valueX = console.ReadLine();
        instance.valueY = console.ReadLine();
        return instance;
    }
    

    Again without seeing your code it's hard to see where you may be going wrong but the above should help somewhat.


  • Registered Users Posts: 981 ✭✭✭fasty


    john47832 wrote: »
    Ok so I have about 150 lines of code for one instance ( a lot if cmd line interaction with user) - i'm thinking there has to be an easier way to create the second instance rather than copy n past the code again and rename instance - is there?

    All that code sounds like a class not an instance. An object is an instance of a class.
    SomeClass c1 = new SomeClass();
    SomeClass c2 = new SomeClass();
    

    They both use the same code.


  • Registered Users Posts: 5,112 ✭✭✭Blowfish


    john47832 wrote: »
    Ok so I have about 150 lines of code for one instance ( a lot if cmd line interaction with user) - i'm thinking there has to be an easier way to create the second instance rather than copy n past the code again and rename instance - is there?
    You may be getting confused between 'class' and 'instance of a class'.

    The best way to think of it (at the initial learning stage anyway) is that a class is a blueprint and an instance of a class is a specific object created from that blueprint. Pretend you have a 'Person' class which describes the basics of a human. As it's a blueprint, it'll contain actions that all people do. One of them would be a method called eat(). The Person class would look something like this:
    public class Person {
        Person() {
           // Person constructor - don't need to know what's in here for the example
        }
    
        public void eat() {
            // eat method - don't need to know what's in here for the example
        }
    }
    

    If we want to create a specific person called john47832 from our 'blueprint', all we do is this:
    public class Foo {
        public static void main (String args[]){
            Person john47832 = new Person();
        }
    }
    
    If we then wanted later to create a person called blowfish, we do it in exactly the same way, leaving us with:
    public class Foo {
        public static void main (String args[]){
            Person john47832 = new Person();
            Person blowfish = new Person();
        }
    }
    
    Both john47832 and blowfish are instances of 'Person' and both have the ability to eat, however they are completely independant.

    If we want one of them to actually eat, all we do is reference them by their name and tell them to eat, which turns our code into:
    public class Foo {
        public static void main (String args[]){
            Person john47832 = new Person();
            Person blowfish = new Person();
    
            john47832.eat();
        }
    }
    
    Hopefully that helps.


  • Advertisement
Advertisement