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 6 of 6

Thread: Calling methods from other classes

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Calling methods from other classes

    Hi everyone, this is such a dumb question but I'm doing my best in trying to learn java from a book

    I'm having a hard time with abstraction/modularization, more specifically I want to call on a method in another class.

    Im just messing around here but I have made three classes: Car, Wheels, Shell.
    Car will abstract from Wheels and Shell.

    Now I'm having a compiler error with Car as it does not like the method:

    public int shellSize()
        {
            //return the size of the shell from the object big
            return big.getSize;
        }

    I have tried everything I know to try and figure out why it won't work - turns out I'm stuck and wondered if you could help?

    What I have:

    public class Car
    {
        private Shell big;
        private Wheels blackTyre;
     
        /**
         * Constructor for objects of class Car
         */
        public Car(String shellColour, int shellSize, String wheelColour, String wheelShape, int howMany)
        {
     
            big = new Shell(shellColour, shellSize);
            blackTyre = new Wheels(wheelColour, wheelShape, howMany);
        }
     
        /**
         * Method - Return the size of the shell
         */
        public int shellSize()
        {
            //return the size of the shell from the object big
            return big.getSize;
        }
    }

    and then my Shell class:

    public class Shell
    {
     
        private String colour;
        private int size;
     
        /**
         * Constructor
         */
        public Shell(String whatColour, int whatSize)
        {
           colour = whatColour;
           size = whatSize;
        }
     
        public String getColour()
        {
            return colour;
        }
     
        public int getSize()
        {
            return size;
        }
     
        public void getInfo()
        {
            System.out.println(" The colour of the shell is: " +colour+ " and the size is: " +size);
        }
    }

    Thanks in advance


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,265
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: Calling methods from other classes

    having a compiler error with Car as it does not like the method
    Please copy and paste here the full text of the error message(s).

  3. #3
    Junior Member
    Join Date
    Jun 2011
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Calling methods from other classes

    The error message I see is:

    cannot find symbol - variable getSize
    So then I tried

    public int shellSize()
        {
            //return the size of the shell from the object big
            return big.size;
        }

    And I get the error message:

    size has private access in shell
    And thats where I'm at :/

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,265
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: Calling methods from other classes

    cannot find symbol - variable getSize
    Look at this error message again. The compiler is looking for a missing variable

    What does the Shell class have that is named getSize?
    A variable or a method?

    You reference a method by adding an ending ()

  5. The Following User Says Thank You to Norm For This Useful Post:

    knightknight (June 7th, 2011)

  6. #5
    Junior Member
    Join Date
    Jun 2011
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Calling methods from other classes

    You are incredible, how did I miss that!

    thanks!

    code:

    public int shellSize()
        {
            //return the size of the shell from the object big
            return big.getSize();
        }

    Thanks again!

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,265
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: Calling methods from other classes

    It pays to take a close look at the error message and try to figure out what the compiler is looking for.

Similar Threads

  1. classes and methods??
    By paddy1 in forum Member Introductions
    Replies: 1
    Last Post: May 20th, 2011, 10:02 AM
  2. Calling worker methods in JSF.
    By newbie in forum Web Frameworks
    Replies: 0
    Last Post: March 18th, 2011, 03:21 PM
  3. [SOLVED] Help with Classes/ Instance Methods
    By Stockholm Syndrome in forum What's Wrong With My Code?
    Replies: 8
    Last Post: March 9th, 2011, 06:33 PM
  4. [SOLVED] Handling Errors / calling Error-Catching Methods
    By movsesinator in forum Object Oriented Programming
    Replies: 4
    Last Post: April 6th, 2010, 06:35 AM
  5. Calling for methods
    By soccer_kid_6 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 1st, 2010, 12:13 AM