I'm pretty new to java, and I'm trying to practice declaring classes, but I keep getting the error:
"non-static variable this cannot be referenced from a static context"
The code i'm using is basic tut code, but for some reason I cannot get it to compile
[/code]
What I think that I did wrong, is put the first class in the other class, seeing my ending (}) was wrong..
So would this be correct>?
make a .java and name it AccountDemo.java
make a new .java class, and name it Account.java
"non-static variable this cannot be referenced from a static context"
The code i'm using is basic tut code, but for some reason I cannot get it to compile
Code:
class AccountDemo
{
//======================================================================
public static void main(String args[])
{
// Create an empty account
Account my_account = new Account();
// Deposit money
my_account.deposit(250.00);
// Print current balance
System.out.println ("Current balance " +
my_account.getbalance());
// Withdraw money
my_account.withdraw(80.00);
// Print remaining balance
System.out.println ("Remaining balance " +
my_account.getbalance());
}
//===========================================================================
public class Account
{
protected double balance;
// Constructor to initialize balance
public Account( double amount )
{
balance = amount;
}
// Overloaded constructor for empty balance
public Account()
{
balance = 0.0;
}
public void deposit( double amount )
{
balance += amount;
}
public double withdraw( double amount )
{
// See if amount can be withdrawn
if (balance >= amount)
{
balance -= amount;
return amount;
}
else
// Withdrawal not allowed
return 0.0;
}
public double getbalance()
{
return balance;
}
}
}
What I think that I did wrong, is put the first class in the other class, seeing my ending (}) was wrong..
So would this be correct>?
make a .java and name it AccountDemo.java
Code:
class AccountDemo
{
//======================================================================
public static void main(String args[])
{
// Create an empty account
Account my_account = new Account();
// Deposit money
my_account.deposit(250.00);
// Print current balance
System.out.println ("Current balance " +
my_account.getbalance());
// Withdraw money
my_account.withdraw(80.00);
// Print remaining balance
System.out.println ("Remaining balance " +
my_account.getbalance());
}
}
make a new .java class, and name it Account.java
Code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
public class Account
{
protected double balance;
// Constructor to initialize balance
public Account( double amount )
{
balance = amount;
}
// Overloaded constructor for empty balance
public Account()
{
balance = 0.0;
}
public void deposit( double amount )
{
balance += amount;
}
public double withdraw( double amount )
{
// See if amount can be withdrawn
if (balance >= amount)
{
balance -= amount;
return amount;
}
else
// Withdrawal not allowed
return 0.0;
}
public double getbalance()
{
return balance;
}
}



