Java Fundamentals (iii)

Source: Internet
Author: User
Tags modifiers

1.
Abstract class Name {
private String name;
Public abstract Boolean Isstupidname (String name) {}
}
Heroes, what's wrong with this?
Answer: wrong. The abstract method must end with a semicolon without curly braces.
2.
public class Something {
void DoSomething () {
Private String s = "";
int L = s.length ();
}
}
Is it wrong?
Answer: wrong. No access modifiers (private,public, and protected) can be placed before local variables. Final can be used to modify local variables
(Final as abstract and STRICTFP are non-access modifiers, STRICTFP can only modify class and method instead of variable).
3.
Abstract class Something {
Private abstract String dosomething ();
}
Does this seem like nothing wrong?
Answer: wrong. Abstract methods cannot be modified with private. Abstract methods is to let sub-class implement (Implementation) specific details, how can use private to abstract
How is the method locked up? (In the same vein, the abstract method cannot be added to final).
4.
public class Something {
public int AddOne (final int x) {
return ++x;
}
}
This is more obvious.
Answer: wrong. int x is modified to final, meaning that x cannot be modified in AddOne method.
5.
public class Something {
public static void Main (string[] args) {
Other o = new Other ();
New Something (). AddOne (o);
}
public void AddOne (final other O) {
o.i++;
}
}
Class Other {
public int i;
}
Similar to the above, it's all about the final question, is it wrong?
Answer: Right. In AddOne method, the parameter O is modified to final. If we change the reference of O in AddOne method
(for example, O = new Other ();), which is also wrong as in the previous example. But here is the member of O vairable
(member variable), and O's reference does not change.
6.
Class Something {
int i;
public void dosomething () {
System.out.println ("i =" + i);
}
}
What's wrong with it? I can't see it.
Answer: Right. The output is "i = 0". int I belongs to instant variable (instance variable, or member variable). Instant variable has default value. The default value of int is 0.
7.
Class Something {
final int i;
public void dosomething () {
System.out.println ("i =" + i);
}
}
There is only one place different from the previous one, that is, one more final. Is that all wrong?
Answer: wrong. Final int i is a final instant variable (instance variable, or member variable). The final instant variable has no 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. There seems to be no problem with the call dosomething in Main, after all, two methods are in the same class. But look closely, the main is static. The static method cannot call Non-staticmethods directly. Can be changed to "System.out.println (" s.dosomething () returns "+ s.dosomething ());". Similarly, the static method cannot access the Non-static instant variable.
9.
Here, the file of the something class is called Otherthing.java
Class Something {
private static void Main (string[] something_to_do) {
System.out.println ("dosomething ...");
}
}
This seems pretty obvious.
Answer: Right. No one has ever said that Java's class name must be the same as its file name. However, the public class must have the same name as the file name.
10.
Interface a{
int x = 0;
}
Class b{
int x = 1;
}
Class C extends B implements A {
public void PX () {
SYSTEM.OUT.PRINTLN (x);
}
public static void Main (string[] args) {
New C (). PX ();
}
}
Answer: Error. An error occurs at compile time (the error describes different JVMs with different information, meaning an ambiguous x call, two x matches (as if you were declaring the date directly when you import Java.util and java.sql two packets at the same time). For a variable of the parent class, you can use super.x to make it explicit, whereas the properties of the interface are implicitly public static final. So you can make it clear by a.x.
11.
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 = Newball ("Football");
System.out.println (Ball.getname ());
}
}
This error is not easy to find.
Answer: wrong. "Interfacerollable extends playable, bounceable" no problem. Interface can inherit multiple interfaces, so this is true. The problem is in the interface rollable "ball ball = Newball (" Pingpang "); Any interface variable (interface variable, also called member variable) declared in interface, default to public static final. That is to say "ball ball = new ball" ("Pingpang"); It is actually "public staticfinal ball ball = new ball (" Pingpang ");". In the ball class play () method, "ball = Newball (" Football ");" Changed ball's reference, and here ball from Rollable interface,rollable interface ball is public static final, The final object cannot be changed reference. So the compiler will be in "ball = new ball" ("Football"); " The error is shown here.

Java Fundamentals (iii)

Related Article

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.