static and more ch5_3 Flashcards

1
Q

what is meant by the term static variable?

A
  • static objects are the objects that belong to the class - not to the instances of the class
  • can only be used inside the class
    A static variableis a variable that belongs to the class as a whole, and not just to one object
    •There is only one copy of a static variable per class, unlike instance variables where each object has its own copy
  • all objects of the class can read and change the static variable
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

can a static method access an instance var?

A

no - static methods can only access static variables

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

how to declare static variables

A

private static int mystaticvariable = 0 – initialized and declared at the same time

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

static variables should always be declared private unless ….

A

is is also a defined constant

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

static method

A
can be used without calling an object
A static method still belongs to a class, and its definition is given inside the class definition
- Static methods are invoked using the class name in place of a calling objectreturnedValue = MyClass.myMethod(arguments)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

pitfall: invoking a non static method within a static method

A
  • A static method cannot refer to an instance variable of the class, and it cannot invoke a non-static method of the class
  • A static method has no “this”, so it cannot use an instance variable or method that has an implicit or explicit this for a calling object
  • A static method can invoke another static method,
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

putting main in any class

A
  • can be contained within a regular class definition
  • In this way the class in which it is contained can be used to create objects in other classes, or it can be run as a program
  • A main method so included in a regular class definition is especially useful when it contains diagnostic code for the class
How well did you know this?
1
Not at all
2
3
4
5
Perfectly