• Log in or sign up for an account here: https://www.sythe.org/login

Java Error Help

nurabutt

Active Member
Banned
Joined
Mar 21, 2009
Sythe Gold
0
Messages
187
Location
Florida
Java Error Help
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:
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;
	   	}
	   }
}
[/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

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;
           }
       }
 
Java Error Help
Yes, your brackets were off and you declared the account class as an inner class within the AccountDemo class. The code you posted as a correction should work, although you can still declare both classes within a single source file.
 
Java Error Help
Yes, your brackets were off and you declared the account class as an inner class within the AccountDemo class. The code you posted as a correction should work, although you can still declare both classes within a single source file.

Thank you for the help, sorry for late response forgot about this thread.
 
Runescape Gamblingrunescape gambling
Back
Top