Sunday, July 12, 2009

When trying to build the following java program i get an error?

the code.. this is taking me a little longer to understand then i thought.





class MainApplication


{





public static void main(String args[])


{





Mammal dog = new Mammal();





String red = "red";





String colorReturned;





dog.changeColor(red);





colorReturned = dog.getColor();





System.out.println(colorReturned);


}


}





public static class Mammal


{


private String color;





public void changeColor(String newColor)


{


color = newColor;


}





public void growHair()


{


System.out.println("Hair Growing");


}





public String getColor()


{


return color;


}





}





----------Error------





init:


deps-jar:


Compiling 1 source file to C:\it1050\JavaApplication6\build\classes


C:\it1050\JavaApplication6\src\javaapp... class Mammal is public, should be declared in a file named Mammal.java


public static class Mammal


1 error


BUILD FAILED (total time: 0 seconds

When trying to build the following java program i get an error?
Like it says, each public class needs to be in its own .java file with the same name as the class. Also, the "static" on your Mammal class is unnecessary. If Mammal doesn't need to be accessed by anything but MainApplication, you could just make Mammal private; otherwise, move it to a new file.
Reply:Suggest you move the Mammal class to a new file called Mammal.java, compile that and then compile the original class.


No comments:

Post a Comment