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. class Person {
  9.  
  10. static String staticName = "Peter";
  11.  
  12. // Always use getters and setters, this is a toy example.
  13. String memberName = "";
  14.  
  15. public Person(String name) {
  16. staticName = name;
  17. this.memberName = name;
  18. }
  19.  
  20. public static void main (String[] args) throws java.lang.Exception {
  21. Person peter = new Person("Peter");
  22. Person paul = new Person("Paul");
  23.  
  24. System.out.println(peter.staticName); // Paul... but this is Peter!
  25. System.out.println(paul.staticName); // Paul
  26.  
  27. System.out.println(peter.memberName); // Peter, member variable has the correct name
  28. System.out.println(paul.memberName); // Paul
  29.  
  30. }
  31. }
Success #stdin #stdout 0.07s 380160KB
stdin
Standard input is empty
stdout
Paul
Paul
Peter
Paul