Results 1 to 5 of 5
  1. #1
    kashyapkeshav is offline Member
    Join Date
    Aug 2012
    Posts
    10
    Rep Power
    0

    Default Question on class

    Hi all ,

    i have started learning JAVA i have one basic question plz clarify this and help me understand.
    Question :
    In the program has only one class but whats the necessity od creating an instance of dat class ?
    is it mandatory to create the instance of a class to access them ?

    please help..

    public class Dog {
    String name;
    public static void main(String[] args){
    Dog dog1=new Dog();
    dog1.bark();
    dog1.name="bart";

    //making a dog array

    Dog[] mydogs=new Dog[3];
    mydogs[0]=new Dog();
    mydogs[1]=new Dog();
    mydogs[2]=new Dog();

    mydogs[0].name="fred";
    mydogs[0].name="Boss";
    System.out.println("last dog name is ");
    System.out.println(mydogs[2].name);

    int x=0;
    while(x<mydogs.length){
    mydogs[x].bark();
    x=x+1;


    }


    }
    private void bark() {
    System.out.println("---------");

    }
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: Question on class

    Don't you read the responses posted in threads you start?
    EDITOR Doesnt contain main type error

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    kashyapkeshav is offline Member
    Join Date
    Aug 2012
    Posts
    10
    Rep Power
    0

    Default Re: Question on class

    Darry,

    Program has a main function , so no question of this error coming up.

    Am looking for a reason why there was an instance created to access the members of the same class ..i just wanna kow the purpose if any specific ?
    public class Dog {
    String name;
    public static void main(String[] args){
    Dog dog1=new Dog();
    dog1.bark();
    dog1.name="bart";

    thanks
    keshav

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Question on class

    Please read Darryl's links, especially the one on how to use code tags. Most here appreciate it if you put in the effort to make it easy to read your code, and this will likely get you better answers quicker.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Question on class

    Java is an object oriented language.
    So it is built with the idea of objects in mind...
    There's not really much more that can be said about it.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. class question
    By Maverick83 in forum New To Java
    Replies: 1
    Last Post: 06-19-2011, 07:34 AM
  2. OO Class Theory Question
    By Dark in forum New To Java
    Replies: 22
    Last Post: 04-20-2011, 02:16 PM
  3. Abstract class question
    By McChill in forum New To Java
    Replies: 4
    Last Post: 02-27-2009, 06:52 AM
  4. I have some question about jar and class??
    By low224 in forum New To Java
    Replies: 2
    Last Post: 01-01-2009, 04:02 AM
  5. question regarding class
    By kavithas in forum New To Java
    Replies: 4
    Last Post: 11-16-2007, 09:12 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
  •