Results 1 to 3 of 3
  1. #1
    BlankFile is offline Member
    Join Date
    Feb 2012
    Posts
    1
    Rep Power
    0

    Default Use classes inside of other classes

    Hello forumers ! I've got a problem.

    The "exercice":

    1 class called "Person"
    name, last name and age of every person, with their getters and setters.
    1 class called "Codes"
    int code, String rank. Here I will put the codes for every person.
    1 main class called "Simulator"
    Instanciate new persons and show system.out.print messages.

    What I need is to instanciate "Codes" inside of "Person" (Like that: private Codes code and use it in the Simulator. For example:

    CLASS PERSON
    private String name = "";
    private int age = 0;
    private String lastname = "";
    private Codes code;

    [... getters ... setters ...]

    CLASS CODES
    private int code = 0;
    public void setCode(int c){
    code = c;
    }

    public int getCode(){
    return code;
    }

    CLASS SIMULATOR MAIN
    person p1 = new person();
    p1.setName("John");
    p1.setLastName("Petrucci");
    p1.setEdad(43);
    p1.Codes.code ?????

    System.out.println("Name: "+p1.getName()+" "+p1.getLastName()+" and the age is: "+p1.getAge());
    System.out.println("The code is: "p1.Codes.code?????" );


    Don't really know how to use classes inside another classes that aren't the main class.

    Please help !!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Use classes inside of other classes

    how to use classes inside another classes that aren't the main class.
    It shouldn't make any difference where you use a class. It should work the same way.
    Your coding syntax is not correct java so its hard to understand what you are trying to do.
    Can you create a correctly written program that shows what you are trying to do.

    What is this statement trying to do:

    p1.Codes.code

    It looks like you are trying to access the member codes in the Person class and then access the member code in that the Codes class.

    It is hard to discuss your problem because of the confusion between class names and member names.
    Your variable and class names do not follow naming conventions: classes begin with Uppercase, members with lower case.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Use classes inside of other classes

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Inner Classes...
    By Splanger in forum New To Java
    Replies: 1
    Last Post: 07-03-2011, 10:50 PM
  2. A little help with classes.
    By ThrashingBoy in forum New To Java
    Replies: 8
    Last Post: 06-10-2010, 09:52 AM
  3. Help with classes
    By gnarly hogie in forum New To Java
    Replies: 14
    Last Post: 10-10-2008, 02:29 PM
  4. Help with classes
    By freswood in forum New To Java
    Replies: 5
    Last Post: 04-21-2008, 03:28 PM
  5. When do we use inner classes?
    By cruxblack in forum New To Java
    Replies: 5
    Last Post: 08-10-2007, 05:00 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
  •