Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

unable to make new object

Status
Not open for further replies.

asm1

Technical User
Oct 9, 2003
77
GB
Hi all, I am trying to teach my self some java to enhance my website which at the moment only uses DHTML, php and JavaScript. I am currently trying to make a new object of an account (I think) ie I want to add a new account number, amount and name but I get a duplicate of the first printed to the output. Any ideas. Here is the code, thanks in advance
Code:
Account.java
public class Account
{
	public static void main(String[]args)
	{
		//cereate new account
		Account1 myAccount=new Account1(1000, 2000, "Tony");
		
		//list the excisting accounts
		int accountNumber=myAccount.getAccountNumber();
		int accountBalance=myAccount.getBalance();
		String accountHolder = myAccount.getHolder();
	
		System.out.println("Account Number " +accountNumber);
		System.out.println("Account Balance "+accountBalance);		
		System.out.println("Account Holder " +accountHolder);
		
		Account1 myAccount1=new Account1(1001, 2000, "Mark");		
		accountNumber=myAccount.getAccountNumber();
		accountBalance=myAccount.getBalance();
		accountHolder = myAccount.getHolder();		
		System.out.println("Account Number1 " +accountNumber);
		System.out.println("Account Balance1 "+accountBalance);		
		System.out.println("Account Holder1 " +accountHolder);
	
		
	
	}
}

  Account1.java

public class Account1
{
	private int theAccountNumber;
	private int theBalance;
	private String theHolder;
	
	public Account1(int accN, int initBal, String name)//constructor method
	{
		this.setAccountNumber(accN);
		this.setBalance(initBal);
		this.setHolder(name);
	}
	
	public void setAccountNumber(int aNumber)//modifier method
	{
		theAccountNumber=aNumber;
	}
	
	public void setBalance(int anAmount)
	{
		theBalance=anAmount;
	}
	
	public void setHolder(String aName)
	{
		theHolder=aName;
	}
	
	public int getAccountNumber()
	{
		return theAccountNumber;
	}
	
	public int getBalance()
	{
		return theBalance;
	}
	
	public String getHolder()
	{
		return theHolder;
	}
}
 
Account1 myAccount1=new Account1(1001,2000, "Mark");
accountNumber=myAccount.getAccountNumber();
accountBalance=myAccount.getBalance();
accountHolder = myAccount.getHolder();

you are using the myAccount object twice, if you want output to display the details for 'Mark' you will need to do.

Account1 myAccount1=new Account1(1001, 2000, "Mark");
accountNumber=myAccount1.getAccountNumber();
accountBalance=myAccount1.getBalance();
accountHolder = myAccount1.getHolder();
 
thank you for your promt reply. that worked but why did that work? cannot just see what happend here. thx again
 
right i think i get it, the object had to have a new name but i could still use the methods via the Account1 constructor, is that roughly correct?
Code:
Account1 hisAccount=new Account1(1001, 2000, "Mark");		
		accountNumber=hisAccount.getAccountNumber();
		accountBalance=hisAccount.getBalance();
		accountHolder = hisAccount.getHolder();		
		System.out.println("Account Number1 " +accountNumber);
		System.out.println("Account Balance1 "+accountBalance);		
		System.out.println("Account Holder1 " +accountHolder);
		System.out.println(" ");
		
		Account1 herAccount=new Account1(1002, 500, "Sarah");		
		accountNumber=herAccount.getAccountNumber();
		accountBalance=herAccount.getBalance();
		accountHolder = herAccount.getHolder();		
		System.out.println("Account Number1 " +accountNumber);
		System.out.println("Account Balance1 "+accountBalance);		
		System.out.println("Account Holder1 " +accountHolder);


 
Account1 hisAccount=new Account1(1001, 2000, "Mark");
Account1 herAccount=new Account1(1002, 500, "Sarah");

What happens is that two completely seperate variables are created, both of which are an Account1 object.

This means that they can both use exactly the same methods as one another, but the contents of both instances are separate from one another.

I hope it sort of makes sense.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top