Java Programmer Interview Trap Encyclopedia (5)

Source: Internet
Author: User
7.

Class Something {
final int i;
public void dosomething () {
System.out.println ("i =" + i);
}
}

There is only one place different from the one above, that is, one more final. Is that a mistake?

Answer: wrong. Final int i is a final instant variable (instance variable, or called member variable). The final instant variable does not have default value and must be given a definite value before the end of the constructor (constructor). Can be modified to "final int i = 0;".

8.

public class Something {
public static void Main (string[] args) {
Something s = new Something ();
System.out.println ("s.dosomething () returns" + dosomething ());
}

Public String dosomething () {
Return ' do something ... ';
}
}

It looks perfect.

Answer: wrong. It looks like there's nothing wrong with call dosomething in Main, after all, two methods are in the same class. But look closely, main is static. Static method cannot directly call non-static methods. Can be changed to "System.out.println (" s.dosomething () returns "+ s.dosomething ());". Similarly, static method cannot access non-static instant variable.

9.

Here, the something class file is named Otherthing.java

Class Something {
private static void Main (string[] something_to_do) {
System.out.println ("Do something ...");
}
}

This seems to be obvious.

Answer: Right. No one has ever said that the Java class name must be the same as its filename. But the name of the public class must be the same as the file name.

10.

One of the hardest questions today:

Interface Playable {
void Play ();
}

Interface Bounceable {
void Play ();
}

Interface Rollable extends playable, bounceable {
Ball Ball = new Ball ("Pingpang");
}
Class Ball implements rollable {
private String name;
Public String GetName () {
return name;
}

Public Ball (String name) {
THIS.name = name;
}

public void Play () {
Ball = new Ball ("Football");
System.out.println (Ball.getname ());
}
}

This mistake is not easy to find.

Answer: wrong. "Interface Rollable extends playable, bounceable" no problem. Interface can inherit multiple interfaces, so this is true. The problem is in the interface rollable "Ball Ball = new Ball (" Pingpang ");". Any interface variable (interface variable, also known as a member variable) declared in interface, is public static final by default. That is to say "Ball Ball = new Ball" ("Pingpang"); is actually "public static final Ball Ball = new Ball (" Pingpang ");". In the play () method of the ball class, "ball = new Ball (" Football ");" Changed the reference of ball, and here ball from rollable interface,rollable interface is public and final, The final object cannot be changed reference. The compiler will therefore be in the "ball = new Ball (" Football "); This shows the error.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.