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

Thread: Simple question regarding objects.

  1. #1
    Member
    Join Date
    Sep 2012
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Simple question regarding objects.

    Hey guys. I'm having trouble on a beginners java assignment, and not touching Java in two years has not helped at all.

    My question is relatively easy and is for beginners;

    I am to create a main.java class which I have to define a circle object and instantiate it, then using instance methods to call upon the results from Circle.java class to Main.java class.

    How do I call upon the Circle.class object? I'm supposed to ask the user to input data from the main.java class, where the circle.java class uses the input data to calculate results, which then I have to use instance methods to call upon the results.

    I thought to call upon an object it would be:

    Circle c = new Circle();

    However the error I get is:

    constructor circle in class Circle cannot be applied to given types;
    required: double
    found: no arguments
    reason: actual and formal argument lists differ in length

    Is it wrong because instead of passing no arguments, I was supposed to put (double radius)?

    Creating Objects (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    I am using this website to try and figure it out myself, but I get confused because:

    my first step is to define the object, which would be the Circle c = new Circle(); yes? in the website they give an example to: Point originOne = new Point(23, 94);

    I am then to instantiate it, which creates an instance of the object. However, they give the same example for instantiating which is: Point originOne = new Point(23, 94);

    So I'm a bit confused as to what the difference is between defining an object and instantiating it.


    EDIT:

    I think I got the defining and instantiating the object correctly, I changed the code to:

    Circle info;

    double radius;
    radius = getRadius();

    info = new Circle (radius);

    which covers the defining and intiantiazing, but how do I output the results as an instance method?
    Last edited by alex067; September 10th, 2012 at 10:16 PM.


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Simple question regarding objects.

    Alex, I appreciate you're in need of help, but you've already started 4 threads surrounding this assignment.
    Unless you do some core reading of Java, you're going to struggle big time with future work.

    If you specify that the circle class constructor takes 1 and only 1 parameter of type double (as has been done in the Circle class), then you need to programmatically construct that object with a specified number, or prompt one from the user, using mechanisms such as the Scanner class.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Member
    Join Date
    Sep 2012
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simple question regarding objects.

    I read the links you gave me, and I'm very sorry in advance for the multiple threads.

    I've extensively read the links, but I'm just confused on the instance methods.

    objectReference.variableName is the format they provide, so to provide my results as an instance method, wouldn't I do:

    system.out.println(" your results are " + Circle.getCircumference());
    I get the error:
    non-static method getCircumference() cannot be referenced from a static context

    Where Circle is the circle.java class and getCircumference is the method from Circle.java
    Last edited by alex067; September 10th, 2012 at 10:28 PM.

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Simple question regarding objects.

    Quote Originally Posted by alex067 View Post
    Is it wrong because instead of passing no arguments, I was supposed to put (double radius)?
    Your constructor for the Circle class requires the radius and you did not supply one.




    Quote Originally Posted by alex067 View Post
    So I'm a bit confused as to what the difference is between defining an object and instantiating it.
    Nothing really.
    Declare a variable:
    <type> <name>;
    int i;
    double d;
    MyObject o;

    Define a variable:
    i = 3;
    d = 3.0;
    o = new MyObject();
    i and d are said to be defined, and o is said to be instantiated, but basically the same thing happens in all 3 cases. A value of <type> is stored in the memory location pointed to by <name>.




    Quote Originally Posted by alex067 View Post
    I think I got the defining and instantiating the object correctly, I changed the code to:

    Circle info;

    double radius;
    radius = getRadius();

    info = new Circle (radius);
    Yes that would be correct. A shorthand version to that would be:
    info = new Circle(getRadius());

  5. #5
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Simple question regarding objects.

    Quote Originally Posted by alex067 View Post
    system.out.println(" your results are " + Circle.getCircumference());
    I get the error:
    non-static method getCircumference() cannot be referenced from a static context
    Circle.getCircumference() would only work if getCircumference() was a static method of the Circle class. What you need is an object of that class, like:
    Circle myCircle = new Circle(8.0); Then you can say myCircle.getCircumference()
    The difference being that if the method is a static method of the class, then any object of the class will use the same (static) method. When it is an instance method, each object has to use it's own version of the method, and the way to access that method is through an object of that class.

Similar Threads

  1. Question regarding objects
    By alex067 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 10th, 2012, 06:22 PM
  2. [Question] Objects instantiated within objects.
    By Xerosigma in forum Object Oriented Programming
    Replies: 6
    Last Post: April 25th, 2012, 10:53 AM
  3. Question Dynamically create objects, set value and call
    By buntyindia in forum Java Theory & Questions
    Replies: 1
    Last Post: May 25th, 2011, 07:50 AM
  4. java question on objects
    By joe98 in forum Threads
    Replies: 2
    Last Post: April 12th, 2011, 03:54 PM
  5. not so simple, simple swing question box
    By wolfgar in forum AWT / Java Swing
    Replies: 2
    Last Post: November 20th, 2009, 03:47 AM