Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By Norm

Thread: objects from non static class

  1. #1
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    151
    Rep Power
    3

    Default objects from non static class

    I read that whenever we have a non-static class, in order to instantiated, the object needs to be created for the class type.

    However lets say I have public class pear. Why does the code new pear(); instantiate? Is there an object being created and if so how do I access it as it has no name.

    The way I learnt it was: pear a = new pear();


    Thanks for your time

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

    Default Re: objects from non static class

    new pear(); //call the class's constructor. The value returned is not saved in a variable.
    Math.sqrt(334); // value returned is not saved in a variable
    Last edited by Norm; 05-15-2012 at 06:47 PM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    151
    Rep Power
    3

    Default Re: objects from non static class

    if I had a method print, could I use: new pear().print(); ?

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,409
    Rep Power
    25

    Default Re: objects from non static class

    Try it and see what happens.
    KevinWorkman likes this.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,524
    Blog Entries
    7
    Rep Power
    20

    Default Re: objects from non static class

    Quote Originally Posted by stuckonjava View Post
    if I had a method print, could I use: new pear().print(); ?
    Yep, because the expression 'new pear()' returns a pear object; name it X, so your expression is equivalent to the expression 'X.print()' where X is an instantiation of a pear.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Can non-static Inner Class have static fields ?
    By fatabass in forum New To Java
    Replies: 3
    Last Post: 02-05-2012, 09:47 PM
  2. Static and non static class methods question
    By silverglade in forum New To Java
    Replies: 2
    Last Post: 05-14-2011, 10:10 PM
  3. Replies: 6
    Last Post: 02-12-2011, 06:56 AM
  4. referencing objects from static method
    By talktofrank in forum New To Java
    Replies: 4
    Last Post: 10-26-2009, 06:09 PM
  5. Static method cannot make new objects?
    By zerkz in forum New To Java
    Replies: 2
    Last Post: 10-15-2009, 03:17 AM

Posting Permissions

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