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