Click here to Skip to main content
15,891,981 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
C#
public class Sa {
     int rollno;
     String name;
     void insertRecord(int r, String n){  //method
      rollno=r;
      name=n;
     }
     void displayInformation(){System.out.println(rollno+" "+name);}//method
     public static void main(String args[]){
      Sa s2,s1=new Sa();
      //Sa s2=new Sa();

      s1.insertRecord(111,"Karan");
      s2.insertRecord(222,"Aryan");
      s1.displayInformation();
      s2.displayInformation();
     }
}
Posted

1 solution

I suppose you're talking about this part here:
Java
Sa s2,s1=new Sa();


and would like to a.) s2 and s1 point to the same Sa object or b.) have each s1 and s2 point to two different Sa instances. b) would not make any sense at all since there is only one instantiation call (new) in the line you wrote, but you could rewrite it as

Java
Sa s2 = new Sa(), s1 = new Sa();


a) wouldn't work either since each initialization needs its own assignment operator. See above.

You could have found this out by yourself though by reading the language specification.

Cheers!
 
Share this answer
 
Comments
CPallini 13-Nov-14 8:39am    
That's it, my 5.
Sergey Alexandrovich Kryukov 13-Nov-14 15:46pm    
5ed.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900