Constructors
- constructor can't ever, ever, ever, have a return type
- Constructors can't be marked static (they are after all associated with object instantiation), they can't be marked final or abstract (because they can't be overridden).
- Can't use variable args on Constructor
- Foo2(int... x, int t) { } // bad var-arg syntax
- Foo2(int... x, int t) { } // bad var-arg syntax
Variables
- Primitives:
- byte, short, int, long, float, double [8,16,32,64,32,64 bits respectively]
- byte, short, int, long, float, double [8,16,32,64,32,64 bits respectively]
- Reference Variables: Refers to an object
- You can declare one or more reference variables,of the same type, in a single line.
- String s1,s2,s3;
- You can declare one or more reference variables,of the same type, in a single line.
- Instance Variables
- Defined in class but outside methods.
- Can be marked: final, transient
- Cannot be marked: static(becomes class variable), synchronized, strictfp,abstract
- Defined in class but outside methods.
- Class variables
- A class variable is any field declared with the static modifier; this tells the compiler that there is exactly one copy of this variable in existence, regardless of how many times the class has been instantiated. A field defining the number of gears for a particular kind of bicycle could be marked as static since conceptually the same number of gears will apply to all instances. The code static int numGears = 6; would create such a static field. Additionally, the keyword final could be added to indicate that the number of gears will never change.
- A class variable is any field declared with the static modifier; this tells the compiler that there is exactly one copy of this variable in existence, regardless of how many times the class has been instantiated. A field defining the number of gears for a particular kind of bicycle could be marked as static since conceptually the same number of gears will apply to all instances. The code static int numGears = 6; would create such a static field. Additionally, the keyword final could be added to indicate that the number of gears will never change.
- Local Variables
- Should be initialized
- Live on stack
- Exists only during the method scope.
- Only final modifier applicable.
- When you define a local variable same as instance variable it is called Shadowing.
- Should be initialized
- Enums
- Restricts a variable to predefined set of values.
- enums can be declared as their own class, or enclosed in another class, and that the syntax for accessing an enum's members depends on where the enum was declared.
- Cannot declare enums in methods.
- An enum is a much safer and more flexible way to implement constants than was possible in earlier versions of Java.
- Restricts a variable to predefined set of values.
No comments:
Post a Comment