Chapter 1 - Declarations and Access Control Flashcards

To pass the SCJP 6 exam

1
Q

Access Modifiers Members can use

A

public
protected
default
private

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Access Modifiers Classes can use

A

default

public

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Class

A

A template that describes the kinds of state and behavior that objects of this type support

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Object

A

When JVM encounters new keyword, make an instance of that class. Object will have its own state, and access to all behaviors defined by its class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

State

A

Each object has its own unique set of instance variables as defined by the class. All instance variables make up the objects state

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Inheritance

A

Code in one class can be reused in other class. Superclasses are extended to more specific sub classes. Subclass inherits accessible instance variables and methods.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Interface

A

100-percent abstract superclass that identified the methods a subclass must support, but not how they must be supported.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Legal identifiers

A
  1. start with a letter, $, or connecting character like a _. Identifiers CANNOT start with a number
  2. after 1st character, can contain letters, $, connecting characters or numbers
  3. no limit to # of characters
  4. cant use JAVA keywords
  5. Identifiers are CASE sensitive
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Classes and interfaces

A

First letter should be Capitalized, if more than 1 word, use camelcase.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Methods

A

First letter should be lowercase, then normal camelCase

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Variables

A

start with lowercase, use camelCase

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

CONSTANTS

A

UPPER_CASE. use underscores for more than 1 word.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

JavaBeans

A

Java classes that have properties. Properties are private instance variables only accessible by getter and setter methods

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

JavaBeans Naming Rules #1

A

If property is NOT boolean, methods prefix must be get, example getSize()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

JavaBeans Naming Rules #2

A

If property is boolean, the getter method is get or is. For example getStopped() or isStopped()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

JavaBeans Naming Rules #3

A

Setter method must prefex set, example setSize()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

JavaBeans Naming Rules #3

A

Setter methods must be marked public, with void return type

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

JavaBeans Naming Rules #4

A

Getter methods must be marked public, take no arguments, and have a return type that matches the argument type of setter method for that property

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

JavaBean Listener Naming Rules #1

A

Listener methods names used to register a listener, must use the prefix add, followed by listener type: addActionListener()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

JavaBean Listener Naming Rules #2

A

Listener methods used to remove a listener must use the prefix remove

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

JavaBean Listener Naming Rules #3

A

Listener method names must end with the word “Listener”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Source File Declaration Rules #1

A

There can be only one public class per source code file

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Source File Declaration Rules #2

A

Comments can appear at the beginning or end of any line in the source code

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

Source File Declaration Rules #3

A

If there is a public class in a file, the name of the file must match the name of the public class. Example: public class Dog { } must be in source file as Dog.java

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

Source File Declaration Rules #3

A

If class is part of package, the package statement must be the first line in source file, before any import statements

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

Source File Declaration Rules #3

A

If there are import statements, they must go between the package statement and the class declaration. If NO package statement, import statements MUST be first line of source

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

Source File Declaration Rules #4

A

import and package statements apply to all classes within a source code file.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

Source File Declaration Rules #5

A

A file can have more than one nonpublic class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

Source File Declaration Rules #6

A

Files with no public classes can have a name that does not match any of the classes in the file.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

Modifiers fall into two categories

A

Access modifiers: public, protected, private

Non-access modifiers: strictfp, final, and abstract

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

Class Access - 3 things

A

When code has access to another class:

  1. Create an instance of class
  2. Extend class
  3. Access certain accessable methods and variables
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

Default Access

A

Class has no modifier preceding it in the declaration. Think of it as package-level access. Classes with default can only be seen by classes within same package.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

Public Access

A

A class declared as public gives all classes from all packages access to the public class.If public class you are accessing is in different package, must import the public class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

Can a class be marked as final and abstract

A

NO

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

Final Class

A

final class means the class can’t be subclassed. Guarantees none of the methods in the class will ever be overridden

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
Q

Abstract class

A

can NEVER be instantiated. Methods in abstract class end in semicolon rather than curly pair brace {}. If a single method is marked as abstract, the WHOLE class must be declared as abstract

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
37
Q

Interface

A

declares what a class can do, without saying anything about HOW the class will do it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
38
Q

Interface

A

implicitly public and abstract

must NOT be marked as static

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
39
Q

Interface

A

all variables in interface must be public, static, and final. They can only declare CONSTANTS, not instance variables.

40
Q

Interface legal declarations

A

public abstract interface Rollable { } //abstract redundant

public interface Rollable { }

41
Q

Interface method modifiers

A

public and abstract modifiers are redundant but acceptable.

42
Q

Interface constants

A

they must be public, static, and final, but done have to be declared that way. You CANNOT change the value of a interface CONSTANT!

43
Q

Interface constants

A

any combination of LEGAL modifiers is OK, as is using no modifiers at all.

44
Q

Interface constants

A

can NEVER be given a value by the implementing or any other class.

45
Q

Subclass

A

if a subclass inherits a member, it’s exactly as if the subclass actually declared the member itself

46
Q

Private Members

A

Members marked private can’t be accessed by code in any class other than the class in which the private member was declared.

47
Q

Private Members

A

you can declare a matching method in a subclass the same as the superclass marked private and its just as if the superclass private method does NOT exist.

48
Q

Can a private method be overridden by a subclass?

A

Technically NO. CAREFUL. Subclass method with same name as private superclass method makes it look and feel as if overwritten.

49
Q

Protected and Default Methods

A
default member may be accessed only if the class accessing it is in the same package
protected members: may be accessed by inheritance even if subclass is different package
50
Q

Protected and Default methods

A

behavior differs only when talking about subclasses

51
Q

Protected Methods

A

Protected: accessible to all other classes inside package and inheritable by any subclasses outside the package

52
Q

Protected Method behavior once a subclass has inherited it

A

The member become PRIVATE to any code outside the subclass, with exception of subclasses of subclass.

53
Q

Final Method

A

Final keyword prevents a method from being overridden in a subclass.

54
Q

Final Arguments

A

Method arguments are essentially the same as variable declarations that appear in parenthesis.If arg defines var as final, it cannot be modified in the method.

55
Q

Abstract Methods

A

A method that has been declared, but not implemented; has NO functional code. Ends with a semi colon instead of a curly brace.

56
Q

Abstract Methods in Public class

A

Will NOT compile if even one method is defined as abstract in a class. You can have abstract class with no abstract methods.

57
Q

Abstract Method subclass inheritance

A

The first concrete subclass of an abstract class MUST implement all abstract methods of the superclass, unless the subclass is ALSO abstract.

58
Q

Methods can never be marked as

A

abstract and final, or both abstract and private…. it just would’t make SENSE!

59
Q

Private Methods

A

cannot be overridden, so they CANNOT be marked abstract.

60
Q

Abstract Method

A

can never be combined with the static modifier

61
Q

Synchronized Modifier

A

method can only be accessed by one thread at a time. Can ONLY be applied to methods,NEVER variables nor classes.

62
Q

Native Methods

A

method is implemented in platform dependent code like C. Must end with semicolon indicating there is no implementation.

63
Q

StrictFP Methods (Floating point)

A

can declare individual methods and or classes as strictfp, but NEVER variables.

64
Q

Constructor

A

Every time you make a new object, at least one constructor is invoked

65
Q

Constructor

A

Must have the same name as the class in which they are declared

66
Q

Constructors

A

Can’t be marked static, final or abstract

67
Q

Primitives

A

Eight types, char, boolean, byte, short, int, long, double, or float. Once declared a primative type can never be changed.

68
Q

Reference Variable

A

Declared to be a specific type, and that type can never change.

69
Q

Primitive sequence from small to big

A

Byte, short, int, long, double, float

70
Q

Primitive number types are all signed

A

That effects their ranges… First bit: 0=positive, 1=negative

71
Q

Range of numeric primatives

A
byte 8
short 16
int 32
long 64
float 32
double 64
72
Q

char

A

contains a single, 16 bit Unicode character

73
Q

instance variables

A

defined inside the class, but outside of any method, and only initialized when class is instantiated

74
Q

instance variables

A

fields that belong to each UNIQUE object

75
Q

instance variables

A

can be marked final

cannot be marked abstract, synchronized, strictfp, native or static

76
Q

local variables

A

variables declared within a method

77
Q

local variables

A

always on stack, not on the heap

78
Q

local variables

A

must be initialized before they are used

79
Q

array declarations

A

arrays are OBJECTS that store multiple values of the SAME type.

80
Q

arrays

A

ALWAYS objects on the HEAP

81
Q

array of Primatives

A

declared by stating the type of elements the array will hold, followed by square brackets on either side of the identifier:
int[] key;
int key [];

82
Q

array of Objects

A

Thread[] threads; // Recommended

Thread threads []; // legal but less readable

83
Q

Multidimensional arrays

A

String[] [] [] occupantName; // 3 dim array

String[] managername[]; // 2 dim array

84
Q

array declaration

A

never legal to include size of array in your declaration.
int [5] scores ; // illegal
JVM doesn’t allocate space until you actually instantiate the array object.

85
Q

Final Variables

A

once initialized to value; CANNOT reinitialize or change it

86
Q

Final Object Reference Variable

A

once assigned can never refer to a different object. The data within object can be modified, but the reference variable cannot.

87
Q

Burn this in

A

there are no final objects, only final references

88
Q

Transient Variables

A

tell JVM to skip the variable when you attempt to serialize it.

89
Q

Volatile Variables

A

can only be applied to instance variables.

90
Q

Can mark as Static

A

methods
variables
a class nested within another class, but not within a method
Initialization blocks

91
Q

static members

A

exist before you ever make a new instance of a class, and will only be ONE copy.

92
Q

enums

A

cannot declare them in a method

93
Q

enums

A
semicolon on declaration is optional
enum CoffeeSize ( BIG, HUGE, SMALL ) ; //; not required
94
Q

enums

A

you can NEVER invoke an enum constructor directly, the enum constructor is automatically invoked.

95
Q

enums

A

you can declare more than one argument to the constuctor. as BIG(i, “A”)