Results 1 to 6 of 6
  1. #1
    jen_ is offline Member
    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0

    Default Problem Creating a class and an object...

    Hi,

    I am incredibly new to Java, and I apologise if this has gone to the wrong board! I didn't quite know what to search for to see if a similar problem was elsewhere.

    I'm running through a textbook and following some examples, however one has me completely stumped. He mentions the code, but not how it fits together!

    So in the book we're create a class:
    public class Account
    {
    String name;
    String address;
    double balance;
    }

    And then afterwards, it mentions to then create objects from this class. I understand the concept and here is an example given:

    class UseAccount{
    public static void main(String[] args) {

    Account myAccount;
    Account yourAccount;

    myAccount=new Account();
    yourAccount=new Account();

    myAccount.name="A B";
    myAccount.address="Someplace";
    myAccount.balance=39.10;
    }


    But what confuses me is that putting this code together (i.e in the same java file) doesn't work! It cannot recognise what an Account is. Can anyone point to where I am going wrong? I know this is such a basic Q, but I don't even know how to find the answer - nothing in the textbook!

    Jen

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,923
    Rep Power
    8

    Default Re: Problem Creating a class and an object...

    You should probably put these in separate Java files.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    jen_ is offline Member
    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0

    Default Re: Problem Creating a class and an object...

    Quote Originally Posted by KevinWorkman View Post
    You should probably put these in separate Java files.
    Hi,

    I have tried putting these in separate files, but still not working! It still can't seem to recognise that Account is a class,

    Jen

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,923
    Rep Power
    8

    Default Re: Problem Creating a class and an object...

    The files need to have the same name as the class. If that still doesn't work, you're going to have to post the exact error here.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    jen_ is offline Member
    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0

    Default Re: Problem Creating a class and an object...

    Sure, my file is called UseAccount.java and it contains:

    class UseAccount {

    public class Account
    { String name;
    String address;
    double balance;
    }

    public static void main(String[] args) {
    Account myAccount = new Account(); //Here is where the error is found
    Account yourAccount;
    myAccount=new Account();
    yourAccount=new Account();
    }
    }

    The error is:
    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    No enclosing instance of type UseAccount is accessible. Must qualify the allocation with an enclosing instance of type UseAccount (e.g. x.new A() where x is an instance of UseAccount).
    at accountPackage.UseAccount.main(UseAccount.java:17)

  6. #6
    penguinCoder is offline Member
    Join Date
    Oct 2012
    Location
    Tempe, Arizona
    Posts
    77
    Blog Entries
    12
    Rep Power
    0

    Default Re: Problem Creating a class and an object...

    It appears to me as if you are just creating a record; correct? You should just keep them in the same file, in my opinion.

    You code is not formatted using the [code] [/code] tags, so I almost didn't spot the fact that you are missing a } at the very end.

    Take the public off of the account class and you will be all good.

    Not trying to 'spoonfeed', but this is how it is suppose to be arranged in one file. This compiles, and works perfect on my computer.
    Java Code:
    class Account
    {
      String name;
      String address;
      double balance;
    }
    
    class UseAccount{
    	public static void main(String[] args) {
    
    		Account myAccount;
    		Account yourAccount;
    
    		myAccount=new Account();
    		yourAccount=new Account();
    
    		myAccount.name="A B";
    		myAccount.address="Someplace";
    		myAccount.balance=39.10;
    	}
    }
    Though just FYI, you could save a little space by declaring and initialing your Account like:
    Java Code:
    Account myAccount = new Account();
    Some cases you don't want to do this, but when you have them one after the other, it makes more sense to do it this way.

Similar Threads

  1. Problems creating a new BinarySearchTree class object
    By EnSlavingBlair in forum New To Java
    Replies: 2
    Last Post: 10-01-2011, 11:27 AM
  2. Replies: 9
    Last Post: 07-29-2011, 02:18 PM
  3. Replies: 3
    Last Post: 09-29-2010, 09:26 AM
  4. Replies: 2
    Last Post: 03-08-2010, 10:29 AM
  5. Creating object of Type Object class
    By venkatv in forum New To Java
    Replies: 3
    Last Post: 07-17-2007, 03:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •