fork download
  1. public class Puppy{
  2.  
  3. int puppyAge;
  4.  
  5. public Puppy(String name){
  6. // This constructor has one parameter, name.
  7. System.out.println("Name chosen is :" + name );
  8. }
  9.  
  10. public void setAge( int age ){
  11. puppyAge = age;
  12. }
  13.  
  14. public int getAge( ){
  15. System.out.println("Puppy's age is :" + puppyAge );
  16. return puppyAge;
  17. }
  18.  
  19. public static void main(String []args){
  20. /* Object creation */
  21. Puppy myPuppy = new Puppy( "tommy" );
  22.  
  23. /* Call class method to set puppy's age */
  24. myPuppy.setAge( 2 );
  25.  
  26. /* Call another class method to get puppy's age */
  27. myPuppy.getAge( );
  28.  
  29. /* You can access instance variable as follows as well */
  30. System.out.println("Variable Value :" + myPuppy.puppyAge );
  31. }
  32. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:1: error: class Puppy is public, should be declared in a file named Puppy.java
public class Puppy{
       ^
1 error
stdout
Standard output is empty