Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder StealthRT's Avatar
    Join Date
    Aug 2006
    Posts
    188
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Unhappy refering class help

    Hey all i am new at java and im having problems with trying to get data from a class file to another one.

    Lets say i have a class file called Occupant:
    Code:
    public class Occupant
      {
          private int adults;
          private int children;
          private int nights;     
    
    public int getAdults()
         {
           return adults;
         }
    
         public int getChildren()
         {
           return children;
         }
         
         public int getNights()
         {
           return nights;
         }
    
    public int getAdultCharge()
         {
           int adultcharge = adults * .10 * nights;
           return adultcharge;
         }
    
    public Occupant(int a, int c, int n)
         {
           adults = a;
           children = c;
           nights = n;
         }
    }
    Now how can i reference that using another class file?
    Code:
    public class Test
    {
        public static void main(String[] args)
        {           
               System.out.println("# of adults: " + Occupant.getAdults());
               System.exit(0);
        }
    }
    But it throws an error. Am i doing it correctly?

    David

  • #2
    Regular Coder Aradon's Avatar
    Join Date
    Jun 2005
    Location
    USA
    Posts
    734
    Thanks
    0
    Thanked 20 Times in 19 Posts
    Well if it throws an error of course not! heh.

    So, whenever you create a class, have a class, see a class, usually there is a constructor within.

    Every class comes with a constructor, even if it's not specified. Lucky for us, there is one specified already!

    Code:
    public Occupant(int a, int c, int n)
         {
           adults = a;
           children = c;
           nights = n;
         }
    You'll noticed that a constructor does not have a return type. Not only that but it also share the name of the class as the method name!

    So in order to use an "Occupant" object, we must first create it!

    Code:
    Occupant apt101 = new Occupant(1, 0, 0);
    We now have an occupant with one adult, not children and 0 nights. We can then get stuff from this object by doing a print!

    Code:
    System.out.println(apt101.getAdults());
    And there you go, object initialization! Let us know if you have any issues.
    "To iterate is human, to recurse divine." -L. Peter Deutsch

  • #3
    Regular Coder StealthRT's Avatar
    Join Date
    Aug 2006
    Posts
    188
    Thanks
    14
    Thanked 0 Times in 0 Posts
    Aradon: But doesnt that just get the data from the (1, 0, 0) and not the other class file?

    David

  • #4
    Regular Coder Aradon's Avatar
    Join Date
    Jun 2005
    Location
    USA
    Posts
    734
    Thanks
    0
    Thanked 20 Times in 19 Posts
    Of course. In order to get the data from this "other class file" you have to have some instantiation of that class. What other class file are you talking about?
    "To iterate is human, to recurse divine." -L. Peter Deutsch

  • #5
    Regular Coder StealthRT's Avatar
    Join Date
    Aug 2006
    Posts
    188
    Thanks
    14
    Thanked 0 Times in 0 Posts
    Ok i have 4 files..

    Occupant.java
    Occupant.class
    Test.java
    Test.class

    When i run test.class i want it to pull the values from Occupant.class but it doesnt...
    Code:
    import java.text.DecimalFormat;
    public class Test
    {
        public static void main(String[] args)
        {
               DecimalFormat number = new DecimalFormat("$###.00");
               Occupant occ = new Occupant(2, 2, 2);
               System.out.println("# of adults: " + occ.getAdults());
               System.exit(0);
        }
    }
    Its says "# of adults: 2" which is correct for the static values i have in there but i need to get the values from the Occupant.class file.

    I figured it went something like this:
    Code:
    Occupant occ = new Occupant(Occupant.getAdults(), Occupant.getChildren(), Occupant.getNights());
    David

  • #6
    Regular Coder Aradon's Avatar
    Join Date
    Jun 2005
    Location
    USA
    Posts
    734
    Thanks
    0
    Thanked 20 Times in 19 Posts
    Those "static values" are the values in that particular Occupant variable. I'm not sure what you mean by other values. What are you expecting to happen?
    "To iterate is human, to recurse divine." -L. Peter Deutsch

  • Users who have thanked Aradon for this post:

    StealthRT (02-10-2008)

  • #7
    Gox
    Gox is offline
    Regular Coder Gox's Avatar
    Join Date
    May 2006
    Location
    Ontario, Canada
    Posts
    392
    Thanks
    2
    Thanked 20 Times in 20 Posts
    Hi StealthRT,

    I think we're a little confused on what you're trying to do here. The way that you've stated things it appears that you want to get information from an instance of the Occupant Class. If this is the case then you should follow Aradon's advice as a method for getting that information.
    Code:
    public static void main(String[] args)
        {
               DecimalFormat number = new DecimalFormat("$###.00");
               Occupant occ = new Occupant(2, 2, 2);
               System.out.println("# of adults: " + occ.getAdults());
               System.out.println("# of children: " + occ.getChildren());
               System.out.println("# of nights: " + occ.getNights());
               System.exit(0);
        }
    Its says "# of adults: 2" which is correct for the static values i have in there but i need to get the values from the Occupant.class file.
    The Occupant class doesn't have any values until you give it some, when you make an instance of that class. If you look at your code you'll see no numbers in you Occupant class, thus how could it return a number if you haven't given it one in the constructor? It can't. They way your code is you have to do things in the above example; Make an instance of an Occupant and give it some values, then you can use this information later if needed.

  • Users who have thanked Gox for this post:

    StealthRT (02-10-2008)

  • #8
    Regular Coder StealthRT's Avatar
    Join Date
    Aug 2006
    Posts
    188
    Thanks
    14
    Thanked 0 Times in 0 Posts
    Ok yeah sorry Aradon & Goz for my noobness...

    I did this:
    Code:
    public int getAdults()
         {
    	adults = 100;
           return adults;
         }
    And it displayed 100 for the getAdults within my Test class. I figured if you put (2,2,2) that it would take that OVER the values in the other class no matter what.

    Thanks again!
    David


  •  

    Posting Permissions

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