I have two classes, User
and Wallet
. If I put them in a single file, everything works fine. How can I put them in separate files like Wallet.java
and User.java
?
When I tried User.java
threw an error because it didn't not know what Wallet
was.
public class Wallet{
float winningAmount;
float bonusAmount;
float depositAmount;
public Wallet(float winningAmount, float bonusAmount, float depositAmount){
this.winningAmount = winningAmount;
this.bonusAmount = bonusAmount;
this.depositAmount = depositAmount;
}
public void debitBonus(float amountToDebit){
this.bonusAmount = this.bonusAmount - amountToDebit;
}
public void debitDeposit(float amountToDebit){
this.depositAmount = this.depositAmount - amountToDebit;
}
public void debitWinnings(float amountToDebit){
this.winningAmount = this.winningAmount - amountToDebit;
}
}
public class User{
int id;
String name;
String email;
String mobileNumber;
boolean isVerified;
Wallet wallet;
User(int id, String name, String email, String mobileNumber,Wallet wallet){
this.id = id;
this.name = name;
this.email = email;
this.mobileNumber = mobileNumber;
this.isVerified = false;
this.wallet = wallet;
}
}
If both files are in the same package it should work immediately.
If they are in different packages, just add an import
statement to import Wallet
into User
:
User.java
package com.domain.package1;
import com.domain.package2.Wallet;
public class User {
Wallet wallet;
}
Wallet.java
package com.domain.package2;
public class Wallet {
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.