Results 1 to 5 of 5
Thread: Question on class
- 01-23-2013, 03:45 AM #1
Member
- Join Date
- Aug 2012
- Posts
- 10
- Rep Power
- 0
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("---------");
}
}
- 01-23-2013, 05:01 AM #2
Re: Question on class
Don't you read the responses posted in threads you start?
EDITOR Doesnt contain main type error
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 01-28-2013, 03:46 AM #3
Member
- Join Date
- Aug 2012
- Posts
- 10
- Rep Power
- 0
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
-
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.
- 01-28-2013, 11:20 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Similar Threads
-
class question
By Maverick83 in forum New To JavaReplies: 1Last Post: 06-19-2011, 07:34 AM -
OO Class Theory Question
By Dark in forum New To JavaReplies: 22Last Post: 04-20-2011, 02:16 PM -
Abstract class question
By McChill in forum New To JavaReplies: 4Last Post: 02-27-2009, 06:52 AM -
I have some question about jar and class??
By low224 in forum New To JavaReplies: 2Last Post: 01-01-2009, 04:02 AM -
question regarding class
By kavithas in forum New To JavaReplies: 4Last Post: 11-16-2007, 09:12 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks