Java Error Help

Discussion in 'Programming General' started by nurabutt, May 16, 2010.

Java Error Help
  1. Unread #1 - May 16, 2010 at 9:09 PM
  2. nurabutt
    Joined:
    Mar 21, 2009
    Posts:
    187
    Referrals:
    0
    Sythe Gold:
    0

    nurabutt Active Member
    Banned

    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;
               }
           }
    
    
     
  3. Unread #2 - May 16, 2010 at 11:21 PM
  4. Jimmy
    Joined:
    Jun 24, 2008
    Posts:
    2,421
    Referrals:
    10
    Sythe Gold:
    25

    Jimmy Ghost
    Retired Sectional Moderator $5 USD Donor

    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.
     
  5. Unread #3 - May 19, 2010 at 1:33 AM
  6. nurabutt
    Joined:
    Mar 21, 2009
    Posts:
    187
    Referrals:
    0
    Sythe Gold:
    0

    nurabutt Active Member
    Banned

    Java Error Help

    Thank you for the help, sorry for late response forgot about this thread.
     
< FireFox MOD | Need some advice on my project >

Users viewing this thread
1 guest


 
 
Adblock breaks this site