Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Error setting up Object. Issue with static vs non-static

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Error setting up Object. Issue with static vs non-static

    Hello, new to the forums. this is the overall code:

    public class Bank {
     
     /**
     * Customer class.
     */
     
    public class Customer {
    	private String firstName, lastName, street, city, state, zip;
     
     
    	/**
    	 * constructor
    	 * pre: none
    	 * post: A Customer object has been created. 
    	 * Customer data has been initialized with parameters.
    	 */
    	public Customer(String fName, String lName, String str, String c, String s, String z) {
    		firstName = fName;
    		lastName = lName;
    		street = str;
    		city = c;
    		state = s;
    		zip = z;
    	}
     
     
    	/**
    	 * Returns a String that represents the Customer object.
    	 * pre: none
    	 * post: A string representing the Account object has 
    	 * been returned.
    	 */
    	 public String toString() {
    		String custString;
     
    		custString = firstName + " " + lastName + "\n";
    		custString += street + "\n";
    		custString += city + ", " + state + "  " + zip + "\n";
    	 	return(custString);
    	}
    }
    /**
     * Account class
     */
     
    public class Account {
    	private double balance;
    	private Customer cust;
     
     
    	/**
    	 * constructor
    	 * pre: none
    	 * post: An account has been created. Balance and 
    	 * customer data has been initialized with parameters.
    	 */
    	public Account(double bal, String fName, String lName, String str, String city, String st, String zip) {
    		balance = bal;
    		cust = new Customer(fName, lName, str, city, st, zip);
    	}
     
     
    	/** 
    	 * Returns the current balance.
    	 * pre: none
    	 * post: The account balance has been returned.
    	 */
    	public double getBalance() {
    	 	return(balance);
    	}
     
     
    	/** 
    	 * A deposit is made to the account.
    	 * pre: none
    	 * post: The balance has been increased by the amount of the deposit.
    	 */
    	public void deposit(double amt) {
    	 	balance += amt;
    	}
     
     
    	/** 
    	 * A withdrawal is made from the account if there is enough money.
    	 * pre: none
    	 * post: The balance has been decreased by the amount withdrawn.
    	 */
    	public void withdrawal(double amt) {
    	 	if (amt <= balance) {
    	 		balance -= amt;
    	 	} else {
    	 		System.out.println("Not enough money in account.");
    	 	}
    	}
     
     
    	/** 
    	 * Returns a String that represents the Account object.
    	 * pre: none
    	 * post: A string representing the Account object has 
    	 * been returned.
    	 */
    	public String toString() {
    		String accountString;
    		NumberFormat money = NumberFormat.getCurrencyInstance();
     
    		accountString = cust.toString();
    		accountString += "Current balance is " + money.format(balance);
    	 	return(accountString);
    	}
    }
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            Account munozAccount = new Account(250, "Maria", "Munoz", "110 Glades Road", "Mytown", "FL", "33445");
            double data;
            NumberFormat money = NumberFormat.getCurrencyInstance();
     
            System.out.println(munozAccount);
     
            System.out.print("Enter deposit amount: ");
            data = input.nextDouble();
            munozAccount.deposit(data);
            System.out.println("Balance is: " + money.format(munozAccount.getBalance()));
     
            System.out.print("Enter withdrawal amount: ");
            data = input.nextDouble();
            munozAccount.withdrawal(data);
            System.out.println("Balance is: " + money.format(munozAccount.getBalance()));	
        }
    }


    The error occurs when I attempt to create the new object Account within the main arguments here:
    Account munozAccount = new Account(250, "Maria", "Munoz", "110 Glades Road", "Mytown", "FL", "33445");

    I'm not sure how to access the generator method, so i'm completely stumped.

    My classmate is having the same exact problem and we've been stuck for a few days, so if you would be willing to check it out, it would be MUCH appreciated! Thanks in advance!
    Last edited by DaedricSHeep; April 3rd, 2014 at 12:14 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Error setting up Object. Issue with static vs non-static

    The error occurs
    Please post the error in its entirety.

  3. #3
    Junior Member
    Join Date
    Apr 2014
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Error setting up Object. Issue with static vs non-static

    Quote Originally Posted by copeg View Post
    Please post the error in its entirety.
    Oops! sorry about that! It's telling me that a "non static variable cannot be referenced from a static context"

    thanks ^^;

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Error setting up Object. Issue with static vs non-static

    Please post the full text of the error message including the line number and the source line.
    All you've said is that there is an error of this type. What we need is the location of the error and the specifics about the statement that the compiler finds fault with.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2014
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Error setting up Object. Issue with static vs non-static

    Sorry ^^;

    Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - non-static variable this cannot be referenced from a static context
    at bank.Bank.main(Bank.java:127)

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Error setting up Object. Issue with static vs non-static

    Ok now post the code for line 127 and point out which variable is the problem.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Apr 2014
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Error setting up Object. Issue with static vs non-static

    The second block of code up there is line 127. I'm not entirely sure which variable is the issue. I'm using a program, NetBeans, and it says the entire line is an issue. As i look at it there isn't an extra or missing variable, there shouldn't be an issue.

    As I type this, my friend found the answer. Apparently the fact that they're all in the same application was messing things up. When she instead created a class library, it solved the issue.

    Thank you so much for looking though, and i'll remember the proper information to provide in the future ^_^

  8. #8
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Error setting up Object. Issue with static vs non-static

    Suggested reading:
    Nested Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    There is no enclosing instance of type Bank, so if you wish to use any inner classes without an enclosing instance you must make that class static (or better yet, consider placing them in their own class files)

  9. #9
    Junior Member
    Join Date
    Apr 2014
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Error setting up Object. Issue with static vs non-static

    Quote Originally Posted by copeg View Post
    Suggested reading:
    Nested Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    There is no enclosing instance of type Bank, so if you wish to use any inner classes without an enclosing instance you must make that class static (or better yet, consider placing them in their own class files)

    Thanks, emailing it to myself now!

Similar Threads

  1. [SOLVED] Game Combat: Fighting one enemy at a time? (Issue with static/non-static?)
    By Rexoa in forum Java Theory & Questions
    Replies: 6
    Last Post: March 2nd, 2014, 07:34 AM
  2. Replies: 6
    Last Post: May 3rd, 2013, 04:25 PM
  3. Replies: 4
    Last Post: November 15th, 2012, 12:09 AM
  4. non static varible from static context error
    By chopficaro in forum What's Wrong With My Code?
    Replies: 12
    Last Post: June 5th, 2012, 07:07 PM
  5. Help setting a private static class variable
    By kyuss in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 26th, 2010, 08:09 AM