-2

enter image description here1.Why the compiler gives a warning on line of "class Human"saying that Multiple markers at this line,The type Human is already defined and Occurrence of 'Human'

2.Even I change line "System.out.println(aPerson.getHeight());" into "aPerson.getHeight();", it still print output in the console window.How does print works in JAVA?

public class HelloWorld{
    public static void main(String[] args){
        Human aPerson = new Human(160);
        System.out.println(aPerson.getHeight());
    }
}

class Human{

   /**
    * constructor
    */
    Human(int h){
        this.height = h;
        System.out.println("I'm born");
    }

   /**
    * accessor
    */
    int getHeight(){
        return this.height;
    }

    int height;
}
Jackie
  • 29
  • 4
  • can you paste exact error message and line number ? – jmj Dec 23 '14 at 04:19
  • 3
    Your code compiles and runs as-is - I'm unable to reproduce any errors or warnings. – FoggyDay Dec 23 '14 at 04:24
  • I have attached a screenshot on the post – Jackie Dec 23 '14 at 04:39
  • 1
    The error suggests that you also defined class Human in another place, such as Test.java – Vitruvie Dec 23 '14 at 04:42
  • Is this because there is a class Human in the Test.java file? – Jackie Dec 23 '14 at 04:42
  • 1
    @Jackie Yes. You attached no access modifier (public/protected/private) to the class, therefore it is visible throughout the entire package, including Test.java and Test1.java, and it is illegal to have two classes of the same name visible at the same time. – Vitruvie Dec 23 '14 at 04:43
  • Doh! Why didn't you *say* that you had another class "Human" out there??? BTW: "Test.java" and "HelloWorld.java" are pretty piss-poor names. SUGGESTION: a) rename `HelloWorld.java => Human.java`, 2) make "Human" public: `public class Human {...}`, c) Move your "static void main()" inside of class "Human". – FoggyDay Dec 24 '14 at 05:25

2 Answers2

1

You attached no access modifier (public/protected/private) to the class, therefore it is visible throughout the entire package, including Test.java (which you have stated also contains a class named Human) and Test1.java, and it is illegal to have two classes of the same name visible at the same time.

If these classes are the same class, simply remove one of the class definitions; you only need to define the class once. If in fact you are defining a different type of Human, rename one of the classes to distinguish them.

Vitruvie
  • 2,327
  • 18
  • 25
0

Two Class can not exist in the same file.Sorry I didn't saw the the snapshot before.

System.out is just an instance of PrintStream. You can check its JavaDoc. Its variability is based on method overloading (multiple methods with the same name, but with different parameters).

This print stream is sending its output to so called standard output.

you'll have more details in this link http://www.programmerinterview.com/index.php/java-questions/how-system-out-println-works/

  • False, you can place as many package-private classes into a file as you want; you're only restricted to one public class. Having multiple classes in a file is atypical, however. – Vitruvie Dec 23 '14 at 05:17
  • Furthermore, while this answers his question he put in the title, it does not solve his problem, which actually has nothing to do with print statements. – Vitruvie Dec 23 '14 at 05:18