Chapter 4 Flashcards

1
Q

Access Modifiers

private
default (package private) access
protected
public

A

■ private: Only accessible within the same class

■ default (package private) access: private and other classes in the same package

■ protected: default access and child classes

■ public: protected and classes in the other packages

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

Static vs. Instance (members(fields or methods))

A

A static member cannot call an instance member.

public class Static {
private String name = “Static class”;
public static void first() { }
public static void second() { }
public void third() { System.out.println(name); }
public static void main(String args[]) {
first();
second();
third(); // DOES NOT COMPILE
} }

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

Type: Static method

Calling: Another static method or variable

Legal?

A

Yes, using the classname

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

Type: Static method

Calling: An instance method or variable

Legal?

A

No

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

Type: Instance method

Calling: A static method or variable

Legal?

A

Yes, using the classname or a reference variable

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

Type: Instance method

Calling: Another instance method or variable

Legal?

A

Yes, using a reference variable

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

Default Constructor

A

Every class in Java has a constructor whether you code one or not. If you don’t include any constructors in the class, Java will create one for you without any parameters. This Java-created constructor is called the default constructor. Sometimes we call it the default no-arguments constructor for clarity.

Having a private constructor in a class tells the compiler not to provide a default no-argument constructor. It also prevents other classes from instantiating the class. This is useful when a class only has static methods or the class wants to control all calls to create new instances of itself.

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

Overloading Constructors

A

You can have multiple constructors in the same class as long as they have different method signatures. When overloading methods, the method name and parameter list needed to match. With constructors, the name is always the same since it has to be the same as the name of the class. This means constructors must have different parameters in order to be overloaded.

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

Order of Initialization (4 rules)

A
  1. If there is a superclass, initialize it first (we’ll cover this rule in the next chapter. For
    now, just say “no superclass” and go on to the next rule.)
  2. Static variable declarations and static initializers in the order they appear in the file.
  3. Instance variable declarations and instance initializers in the order they appear in the file.
  4. The constructor.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly