Sei sulla pagina 1di 8

SCJP Study Notes: Chapter 1 Declarations and Access Control

Chapter 1 Declarations and Access Control 1. Identifiers and JavaBean 1.1 Legal Identifiers Must start with a letter, $ or _ , CANT start with number Legal: _a, $c, __2_w, _$ Illegal: :b, -d, e#, .f, 7g Java Keyword abstract boolean char class double else for goto int interface private protected strictfp super throws transient assert enum break const extends if long public switch try byte continue final implements native return synchronized void case default finally import new short this volatile catch do float instanceof package static throw while

1.2 JavaBeans Standards JavaBeans => help Java developer create component => class that have properties 1. JavaBean Property Naming Rule - if not Boolean, getter method prefix must be get, e.g. getSize( ) - if boolean, getStopped( ) or isStopped( ) - setter method prefix must be set, e.g. setSize( ) - setter method must marked public with void return type, with arg. Represent property value - getter method must marked public with no arg, return type match arg type of setter method 2. JavaBean Listener Naming Rule - register listener prefix with add, eg. AddActionListener( ) - unreg => remove - Listener method name must end with Listener E.g. valid JavaBean Signature: public void setMyValue(int v) public int getMyValue( ) public Boolean isMyStatus( ) public void addMyListener(MyListener m) public void removeMyListener(MyListener m) E.g. Invalid JavaBean Signature: void setCustomer(String s) // must be public public void modfiyMyValue(int v) // cant use modify public void addXListener(MyListenser m) // Mismatch Page 1 of 8

SCJP Study Notes: Chapter 1 Declarations and Access Control

2. Declare Classes 2.1 Source File Declaration Rules - there can be ONLY ONE public class per src. file - package, import, class - a file can have more than 1 non-public class 2.2 Class Declaration and Modifiers Access modifier: public, protected, private, default (package access) Non access modifier: strictfp, final, abstract Public => all package can access, still need to import it Final => cant be subclassed Abstract class => cant be instantiated abstract class Car { private double price; public abstract void goFast( ); {} } 3. Declare Interface Interface => a contract for what a class can do w/o saying how itll do interface Bounceable void bounce( ); interface Bounceable public abstract void bounce( );
implicit

// end with ; instead of

what you declare what compiler see

class Tire implements Bounceable public void bounce( ) { }

ALL interface method must be implemented and mark public

Interface => 100% abstract class => can only have abstract method All interface method are implicitly public and abstract All var. in interface must be public, static and final, i.e. const Interface method must NOT be static Interface cant extend anything except another Interface Interface cant implement another interface Legal interface declaration: public abstract interface Rollable { } public interface Rollable { } Illegal interface method:

Page 2 of 8

SCJP Study Notes: Chapter 1 Declarations and Access Control

final void bounce( ); static void bounce( ); private void bounce( ); protected void bounce( ); Declaring Interface Constant ALWAYS: public static final => can be omitted => CANT change the value no matter how you declare 4. Declare Class Member 4.1 Access Modifier class => default or public (just two) member => public, protected, default, private Public Member - access from diff. package, need to import it first 3 ways of access a method in same class using reference of the class (.operator) in inherited method (no need .operator)

For subclass, if declared public, can be in diff. package Private Member => subclass cant inherit / overriding it Protected and Default Member (Most confusing and complicated) Almost identical EXCEPT protected member can be accessed by subclass in diff. package => the diff. is ONLY in subclass Default access => Package restriction Protected => Package + Kid => ONLY through inheritance package Cert; public class Parent { protected int x = 9; } package Other; import Cert.Parent; class Child extends Parent { CASE 1 public void testit( ) { System.out.println(x); //CASE 2 OK Parent p = new Parent( ); System.out.println(p.x); // ERROR } }

Page 3 of 8

SCJP Study Notes: Chapter 1 Declarations and Access Control

Neighbor => same package of child instantiate a child object => cannot access x CASE 3 Local Variable Access modifier NOT applied to local var., except final From same class Other class in same package From subclass in same package From subclass outside package From non-subclass class outside package Public Y Y Y Y Y Protected Y Y Y Y N Default Y Y Y N N Private Y N N N N

4.2 Non - Access Modifier through inheritance Final Method - final prevent a method from being overridden in subclass cant be modified Final Argument public int getRecord(final int number} { } Abstract Method - if one method declared abstract, the class must be abstract - the first concrete subclass of an abstract class must implement all abstract method - if abstract extends abstract => no need to implement - abstract cant be combined with final or static or private Synchronized Method - the method can be accessed by only one thread at a time - can only be applied to method, NOT var, NOT class public synchronized Record getRecord(int id) { } Method with Variable Argument List (var-args) - must specified type of arg., can be object type - can have other parm, but var-args must be last parm, can only have one var-arg Legal: void func(int x) { } void func(char c, int x) { } void func(MyObj myobj) { } Illegal: void func(int x ) { } void func(int x, char y) { } void func(String s, byte b) { } 4.3 Constructor Declaration Constructor cant have return type Page 4 of 8

within the function x[0], x[1]

SCJP Study Notes: Chapter 1 Declarations and Access Control

class test { protected test ( ) { } // constructor protected void test ( ) { } // method, NOT constructor } - if dont create explicitly, compiler will build one for you - constructor can have normal access modifier, except final, abstract - must take same name as the class - cant mark static, final or abstract class test2 { test2 ( ) { } Legal constructor private test2 (byte b) { } test2 (int x) { } void test2 ( ) { } test ( ) { } test2 (short s); static test2 ( ); } 4.4 Variable Declaration Primitive: Type Bits byte 8 short 16 int 32 long 64 float 32 double 64 Illegal constructor

char => 16 bit unicode Boolean => true/false

Reference var: e.g. Object O, String s1 Instance var: can be marked final, transient, abstract, synchronized, strictfp, native, static, public, private, protected Local var: must be initialized, ONLY final final var: final reference Transient var: JVM will ignore the var when serializing the obj. Volatile var Static var Array => are object int [ ] key; int key [ ]; String [ ] [ ][ ] test; int [5] test; // ERROR 4.5 Declaring Enums enum CoffeeSize { BIG, HUGE, OVERWHELMING }; CoffeeSize cs = CoffeeSize.BIG; String [ ] mgr [ ];

Page 5 of 8

SCJP Study Notes: Chapter 1 Declarations and Access Control

Enums can be declared as their own separate class, or as a class member, however they must not be declared within a method! 1) Declaring an enum outside a class: enum CoffeeSize {BIG, HUGE, SUPER} // cannot be private or protected class Coffee { CoffeeSize size; } public class CoffeeTest1 { public static void main(String[] args) { Coffee drink = new Coffee(); drink.size = CoffeeSize.BIG; // enum outside class } } ** The enum declared like this MUST be public or default 2) Declaring an enum inside a class: class Coffee2 { enum CoffeeSize {BIG, HUGE, OVERWHELMING } CoffeeSize size; } public class CoffeeTest2 { public static void main(String[] args) { Coffee2 drink = new Coffee2(); drink.size = Coffee2.CoffeeSize.BIG; // enclosing class // name required } } ILLEGAL public class CoffeeTest1 { public static void main(String[] args) { enum CoffeeSize { BIG, HUGE, OVERWHELMING } // WRONG! Cannot declare enums in methods Coffee drink = new Coffee(); drink.size = CoffeeSize.BIG; } } Optional Semicolon enum CoffeeSize { BIG, HUGE, OVERWHELMING }; o remember enums are not String or int o each of the enumerated types (BIG, HUGE) are actually instances of CoffeeSize

Page 6 of 8

SCJP Study Notes: Chapter 1 Declarations and Access Control

o think of an enums as a kind of class class CoffeeSize { public static final CoffeeSize BIG = new CoffeeSize("BIG", 0); public static final CoffeeSize HUGE = new CoffeeSize("HUGE", 1); public CoffeeSize(String enumName, int index) { } public static void main(String[] args) { System.out.println(CoffeeSize.BIG); } } Declaring Constructors, Methods and Variables in an enum You could make some kind of a lookup table enum CoffeeSize { // 8, 10 and 16 are "passed" as values to the constructor BIG(8), HUGE(10), OVERWHELMING(16); CoffeeSize(int ounces) { this.ounces = ounces; // assign the value to an instance variable } private int ounces; // an instance variable each enum value has public int getOunces() { return ounces; } } class Coffee { CoffeeSize size; // each instance of Coffee has-a CoffeeSize enum public static void main(String[] args) { Coffee drink1 = new Coffee(); drink1.size = CoffeeSize.BIG; Coffee drink2 = new Coffee(); drink2.size = CoffeeSize.OVERWHELMING; System.out.println(drink1.size.getOunces()); // prints 8 for(CoffeeSize cs: CoffeeSize.values( )) System.out.println(cs + + cs.getOunces( )); // BIG, 8 } } o Every enum has a static method, values( ), that returns an arrya of the enums values in the order theyre declared

Page 7 of 8

SCJP Study Notes: Chapter 1 Declarations and Access Control

o You can NEVER invoke an enum constructor directly. The enum constructor is invoked automatically, with the arguments you define after the constant value Declare enum that looks like an anonymous inner class: enum CoffeeSize { BIG(8), HUGE(10), OVERWHELMING(16) { // start a code block that defines the "body" for this constant public String getLidCode() { // override the method defined in CoffeeSize return "A"; } }; // <-- the semicolon is REQUIRED when you have a body CoffeeSize(int ounces) { this.ounces = ounces; } private int ounces; public int getOunces() { return ounces; } public String getLidCode() { // this method is overridden by the OVERWHELMING constant return "B"; // the default value we want to return for CoffeeSize constants } }

Page 8 of 8

Potrebbero piacerti anche