Question

hello im just trying out some basic java skills.

here is my code

package bankaccount;
public class Test {

    String name;
    int houseno;
    int balance;

    Test (String n,int h,int b) {

        n=name;
        h=houseno;
        b=balance;

    }
   int getBalance(){   

    return balance;
}
   void setBalance(int newBalance){

       newBalance = balance;
   }

}

//the class containing my main

package bankaccount;

/**
 *
 * @author ideapc
 */
public class BankAccount {
Test t1 = new Test("tim",147,5);
    System.out.println(t1.getBalance() + "name =");

    public static void main(String[] args) {

    }

}

I am trying to print the balance of the object t1 (tim) to the console using system.out.println

what am I doing wrong here?

Was it helpful?

Solution

put System.out.println() inside your main() method.

 public class BankAccount {
     public static void main(String[] args) {
      Test t1 = new Test("tim",147,5);
      System.out.println("name = "+t1.getBalance());
     }  
 }

You need to change your constructor of Test too.

Test (String n,int h,int b) {
     this.name=n;
     this.houseno=h;
     this.balance=b;
} 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top