|
public class Class01 { public static void main(String[] args) { int stu1_accNum = 12345, stu2_accNum = 23456; String stu1_name = "Mary", stu2_name = "John"; double stu1_balance = 1000, stu2_balance = 1500; System.out.println( stu1_accNum + " " + stu1_name + " " + stu1_balance); System.out.println( stu2_accNum + " " + stu2_name + " " + stu2_balance); } } |
|
How to run the program:
|
|
Example:
|
|
public class BankAccount { public int accNum; // Account number public String name; // Name of the account holder public double balance; // Balance in the account } |
Explanation:
|
|
|
In Java however ---- (just like the case of arrays - see: click here) ---- defining objects is a two-step process
|
Example:
Step 1: BankAccount stu1; // The type "BankAccount" is the // "object reference" type to a BankAccount object // The variable stu1 contains an address of // a BankAccount object |
|
(This process is exactly the same as that of defining an array in Java)
|
public class BankAccount { public int accNum; // instance variabe: a NEW kind of variables !!! public String name; public double balance; } |
Output:
12345 Mary 1000.0 23456 John 1500.0 |
How to run the program:
|