This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
alex lesta
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


You ask dog1 to bark before you give it a name. What should it print?
 
alex lesta
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50277
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please avoid brightly coloured text, which many people find difficult to read.
 
Bryan Murphs
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Best way to learn Java is to do sample code like above - practice makes perfect
 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And please UseCodeTags next time. It will make your code easier to read.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Thanks for the info guys. bare with me i am a rookie.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic