DALLASCAO.COM

Site of Cao Shouguang, English to Chinese translator

static method

static method
public class SavingsAccount { public static void main(String[] args) { AccountCaculation myAccount = new AccountCaculation(1000).deposit(2000); AccountCaculation yourAccount = new AccountCaculation(2000).deposit(5000); myAccount.printBalance(); yourAccount.printBalance(); System.out.println(AccountCaculation.totalBalance); System.out.println(AccountCaculation.usd2Rmb(myAccount.balance)); } } class AccountCaculation { int balance; static int totalBalance; // static method is per class and cannot access object variables static int usd2Rmb (int usd) { //can access class variables […]Read More

public class and class

public class and class
public class SavingsAccount { public static void main(String[] args) { AccountCaculation myAccount = new AccountCaculation(1000).deposit(2000); AccountCaculation yourAccount = new AccountCaculation(2000).deposit(5000); myAccount.printBalance(); yourAccount.printBalance(); System.out.println(AccountCaculation.totalBalance); } } class AccountCaculation { /** * output: * * 3000 * 7000 * 10000 */ int balance; static int totalBalance; AccountCaculation(int balance) { this.balance = balance; } AccountCaculation deposit(int amount) { […]Read More

learn java: set classname as method type

learn java: set  classname as method type
public class SavingsAccount { /** * output: * * 3000 * 7000 * 10000 */ int balance; static int totalBalance; //constructor SavingsAccount(int balance) { this.balance = balance; } //Classname as method type. User return this SavingsAccount deposit(int amount) { balance += amount; totalBalance = totalBalance + balance; return this; } //method void type void printBalance() […]Read More

Learn java: static variable

Learn java: static variable
public class Car { /** * Car.counter will output 2 */ String make; String model; int numDoors; static int counter; // constructor, applying to all cars Car(String make, String model, int numDoors) { this.make = make; this.model = model; this.numDoors = numDoors; counter++; } //constructor set default numDoors to 4 Car(String make, String model) { […]Read More

Learn java: Contructors vs methods

Learn java: Contructors vs methods
public class Car { /**Output: My car has 4 doors Your car has 3 doors Your car is driven by your wife */ String make; String model; int numDoors; // constructor, applying to all cars Car(String make, String model, int numDoors) { this.make = make; this.model = model; this.numDoors = numDoors; } //constructor set default […]Read More