aspose file tools*
The moose likes Beginning Java and the fly likes can someone explain to me why the output would say null? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "can someone explain to me why the output would say null?" Watch "can someone explain to me why the output would say null?" New topic
Author

can someone explain to me why the output would say null?

alex lesta
Greenhorn

Joined: Apr 08, 2010
Posts: 26

class myDog {
String name;
public static void main(String[] arg){
// make a Dog object and access it
myDog dog1 = new myDog();
dog1.bark();
dog1.name = "Bart";

// now make a Dog aray
myDog[] myDogs = new myDog[3];

// and put some dogs in it
myDogs[0]= new myDog();
myDogs[1] = new myDog();
myDogs[2] = dog1;

// now access the Dogs using the array
// reference

myDogs[0].name = "fred";
myDogs[1].name = "Toby";

// Hmmm.... what is myDogs[2] name
System.out.print("last's dog's name is ");
System.out.println(myDogs[2].name);

// now loop through the array
// and tell all Dogs to bark

int x = 0;
while (x < myDogs.length){
myDogs[x].bark();
x = x + 1;
}
}
public void bark(){
System.out.println(name + " says Ruff");
}

}

This is the output:
null says Ruff
last's dog's name is Bart
fred says Ruff
Toby says Ruff
Bart says Ruff


Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21



You ask dog1 to bark before you give it a name. What should it print?


Steve
alex lesta
Greenhorn

Joined: Apr 08, 2010
Posts: 26
I am new to Java, I am following the book Head first Java. Im on Chap 3 and i was just wondering why it would say null when each has an object.
alex lesta
Greenhorn

Joined: Apr 08, 2010
Posts: 26
Steve Luke wrote:

You ask dog1 to bark before you give it a name. What should it print?

Hey Thanks i was breaking my head trying to figure this out. Like i said i am new to Java
Do yo have any tips on learning how to program with Java
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
Please avoid brightly coloured text, which many people find difficult to read.
Bryan Murphs
Greenhorn

Joined: Oct 18, 2010
Posts: 6
Best way to learn Java is to do sample code like above - practice makes perfect
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19719
    
  20

And please UseCodeTags next time. It will make your code easier to read.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Agreed. Play with lots of code. Start with some books (Head First series are great).
Troll some fora (like these) picking out peoples questions and seeing if you can answer them (and compare your answers with those posted)...
And set a goal. Think of an app you want to write and start writing it - forget about if it has already been written, just use it to gain some experience.

Mainly just find excuses to keep reading and writing code, and whenever possible getting others to read yours so you can get feedback.
alex lesta
Greenhorn

Joined: Apr 08, 2010
Posts: 26
Hey Thanks for the info guys. bare with me i am a rookie.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: can someone explain to me why the output would say null?