CC-SCJP Study Details
- -|
Language Fundamentals
- -|| public static final synchronized double getPrice(int itemCode, Color itemColor) throws Exception {..}(notice order)
An abstract method or a method defined in an interface must end in a semicolon without curly braces:
public abstract void showImage();
The code will compile if you create a method named main() without modifying it as static; however, when you try to
run the class from the command line it will say no main() method exists.
Notice:the method main() is the static method,notice non-static!
Constructors can’t be inherited as other super-class methods are.
The constructor method can’t be final, abstract, synchronized, native, or static.
Notice:the use of the super().(in constrcutor)
- -|| Local variables may only include elements variable type and identifier.(have no such as public static protected...)
Keep in mind that variables can not be synchronized, abstract, or native.
Notice:import java.*; //nothing will be imported,but it may be complied.
true false and null are not keywords.
- -|| It is legal to declare a local variable without initializing it if you do not use the variable.
The class member variables have default value but local variables which are declared in method haven't.
- -|| Notice:command-line argument pass to main():
public class MainTest{
public static void main(String args[]){
for(int i=0;i<args.length;i++){
System.out.println(args[i]);
}
System.out.println("Total words:"+args.length);
}
}
- -|| Notice:Array elements be initialized to the default value when outside the method and in.
Declarations and Access Control
- -|| The solution to this is to first look at the access level of the class. If the class itself will not be visible to
another class, then none of the members will be either, even if the member is declared public. Once it is confirmed
that the class is visible, then it is okay to look at access levels on individual members.
The default class allows other classes within the same package to have visibility to this class.
The default class has visibility to the class which in different package is an abstract class.(could be extended)
- -|| The only access modifier permitted to non-inner classes is public; there is no such thing as a protected or private
top-level class.
- -|| As soon as you supply any constructor with any signature, Java will no longer create a no-arguments default
constructor.
Notice:The default constructor in extends.
In extends,the default superclass constructor is run first,followed by other.
class SuperServer {
public SuperServer() {
System.out.print(" all ");
}
public SuperServer(int y);
this();
System.out.println {" good ");
}
}
class TestServer extends SuperServer{
public TestServer(int y) {
System.out.print(" things ");
}
public TestServer() {
super(10);
System.out.print(" come ");
}
public static void main(String [] args){
TestServer ts = new TestServer(10);
}
}
output:all things
- -|| Notice:The accessing differences with default and protected when in different package.(in extends)
When the member final variable and automatic variable have the same name,about their value.
Notice:The super class member final variable with default can't be accessed by subclass which in another package.
(in extends)
Notice:
class TestServer{
static int users=0;
public void logIn(){
users+=1;
}
}
This class works because a static variable may be modified in an instance method.
class TestServer{
int users=0;
public static void logIn(){
users+=1;
}
}
This class does not compile because a static method may not modify an instance variable.
Notice when there is no instance or static variable but the automatic variable in code.
The automatic variables have not access modifiers,because a method variable can only be used within its method.
- -|| Illegal:
public protected int x;
// At most 1 access modifier
default Button getBtn() { ... }
// "default" isn't a keyword
- -|| Legal be overridden method access
Private default protected public
↓ ↓ ↓ ↓
private default protected public
default protected public
protected public
public
- -|| A final class may not be extended.
A final variable may not be change its value.
A final method may not be overridden.
A final variable must be initialized before it is used.(final literal)
- -|| The static variable is incremented every time the constructor is called, so it is possible to know how many
instances have been created.
A static method has no this.
A static method may not be overridden to be non-static.
A static method may not be overridden.
A non-static method may be overridden by non-static method.
Notice:
Ecstatic e1 = new Ecstatic();
Ecstatic e2 = new Ecstatic();
e1.x = 100;
e2.x = 200;
reallyImportantVariable = e1.x;
The value of reallyIportantVariable is 200,because e1.x and e2.x refer to the same (static) variable.
There are two ways to reference a static variable:
A.Via a reference to any instance of the class
B.Via the class name
The static variable could be modified in the instance method.
No static method could access static variable,but static method must access static variable,other variable can't!
The static block is executed once,at the time the class is loaded.
Notice:the initializer value of static variable.
Notice:mai() is a static method too,can't call non-static method in it.
- -|| A method may not be overridden with a new return type.
class Thing{
void aMethod(){}
}
class SubThing extends Thing{
long aMethod(){return 123L;} //illegal no matter what access modifiers is applied.
}
- -|| It is legal to have an abstract class that contains only non-abstract methods.
An abstract method can't be modified by static!!
When subclass extends an abstract class must achieve all abstract methods which defined in abstract class,otherwise
the class must be declared abstract class.
When class have achieved an interface but not achieve all methods which in this interface,this class must be
declared abstract class,otherwise complie error!!
- -|| Notice:An abstract class can contain the final method.
The final keyword prevents a method from being overridden in a subclass.
A final class can't contain the abstarct method.
If there is the abstract method in a class,the class must be declared abstract.
Operators and Assignments
- -|| 当赋值运算符的操作数为基本类型时,在赋值语句执行后,改变赋值运算符的左操作数值,不会影响赋值运算符右边的操作数值;
当赋值运算符的操作数为引用类型时,在赋值语句执行后,改变赋值运算符的左操作数引用对象的内容,同时改变了赋值运算符右操作引用对象的内容,
因为在赋值语句执行后,左右操作数引用的是同一个对象。
class ValueTest {
public static void main (String [] args) {
int a = 10;
System.out.println("a = " + a);
b = a;
b = 30;
System.out.println("a = " + a + "after change to b");
}
}
output:a = 10
a = 10 after change to b
class Strings {
public static void main(String [] args) {
String x = "Java";
String y = x;
System.out.println("y string = " + y);
x = x + " Bean";
System.out.println("y string = " + y);
}
}
output:y string = Java
y string = Java
As you can see, even though y is a reference variable to the same object that x refers to, when we change x it does
not change y.This is because a brand new String object is created every time we use the + operator to concatenate
two strings.