Results 1 to 5 of 5
  1. #1
    Robomano is offline Member
    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    Default Problem with understanding the use of classes

    Well am pretty new to java,and i have a problem with understanding classes,yes i know they act like templates but my question is when you are creating a new class do you need to instantiate it first??,

  2. #2
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Smile Re: Problem with understanding the use of classes

    I try a pragmatic explanation, so the more advanced ones may correct me if something is technically not completely correct:

    A class is the blueprint, an object is the house build using the blueprint. Maybe that helps you a bit.
    Classes are referred to as "Types" when talking about what kind of object something is.
    So you cannot work with the blueprint if you want to achieve something, you need to build an object first and use that one - objects are referenced by local variables or attributes of other objects.
    So when you create a new _object_ of a certain type you use the "new" statement OR you call a method that does it for you but usually it is the "new" keyword you want to use at the beginning.

    Hope that clears it up a abit - else you should try to give a tutorial a try or use the search function on this forum. ;)
    I like likes!

  3. #3
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: Problem with understanding the use of classes

    Quote Originally Posted by Robomano View Post
    Well am pretty new to java,and i have a problem with understanding classes,yes i know they act like templates but my question is when you are creating a new class do you need to instantiate it first??,
    Maybe I can add a little to Sierra's answer. You need to instantiate a class to create object if you want to access the instance methods or fields of the object. But if you want to access a class methods or class fields (this is also called as static method or static fields) then you don't need to create an instance of this class.

    For example:

    Java Code:
    public class Demo {
        public void doOne() {
            System.out.println("doOne");
        }
    
        public static void doTwo() {
            System.out.println("doTwo");
        }
    
        public static void main(String[] args) {
            // To call doOne() method we need an object of Demo.
            Demo demo = new Demo();
            demo.doOne();
    
            // To call a static method like doTwo() we can call without creating an object first.
            Demo.doTwo();
        }
    }
    Last edited by wsaryada; 12-10-2012 at 11:21 AM.

  4. #4
    Robomano is offline Member
    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    Default Re: Problem with understanding the use of classes

    Wow okay thank you alot,another question though...can an object be used by more than one class??

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,184
    Rep Power
    20

    Default Re: Problem with understanding the use of classes

    Yes.
    That's what happens when you pass an object reference as a parameter to a method.
    eg
    Java Code:
    public class MyClass {
       private List<String> someData = new ArrayList<String>();
       private SomeOtherClass soc = new SomeOtherClass();
       ...
    
       public void someMethod() {
          soc.doSomething(someData);
       }
    }
    That call to doSomething() has taken my List reference, so the soc object can do what it wants with it.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 2
    Last Post: 01-23-2012, 12:50 AM
  2. Problem understanding class statement.
    By martijnburger in forum New To Java
    Replies: 3
    Last Post: 07-22-2011, 12:07 AM
  3. Replies: 1
    Last Post: 05-10-2011, 09:10 AM
  4. difficulty understanding writing classes
    By elecleoalune in forum New To Java
    Replies: 11
    Last Post: 04-18-2011, 04:06 PM
  5. Replies: 9
    Last Post: 09-21-2010, 05:15 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
  •