Designing static methods and fields Flashcards

1
Q

Define a static method !

A

a static method is a method that belongs to the class itself rather than an instance of the class

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

Define class as an entity concept in java !

A

class as an entity concept explain that the definition of a class exists itself in memory before creating any instances of that class, and that entity serves as a blueprint to create instances of that class, you still interact with that class and its static members before creating any instances of it

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

What is the difference between instance variables and instance methods in memory ?

A
  • The code for an instance method is shared among all instances of the class which means that once the source code is compiled into bytecode, the bytcode of the method exists once in memory regardless of how many instances are created .
  • The code for an instance variable is not shared among all instance of the class. Separate memory allocations for different instances are created .
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Explain in great detail what happens in memory when an instance method is called !

A

Function or Method Calls : When a function or method is called within the program, its corresponding stack frame is created, containing information such as parameters, local variables, return address, and saved machine state.

Pushing onto the Stack : The newly created stack frame is pushed onto the call stack, becoming the current top of the stack. This represents the activation of the called function or method.

Execution of Functions or Methods : The program executes the instructions within the called function or method, accessing its parameters, local variables, and other relevant data stored in the stack frame

Nested Calls : If the called function or method makes further function or method calls, additional stack frames are created and pushed onto the stack for each nested call. This results in a hierarchical structure of stack frames representing the nested function or method calls.

Returning from Calls : When a function or method completes execution, its corresponding stack frame is popped off the stack, and control returns to the caller. The program resumes execution at the return address stored in the caller’s stack frame.

Reusing Stack Frames : As functions or methods return, their stack frames are deallocated, and the memory previously occupied by those frames is freed. The call stack dynamically grows and shrinks as function or method calls are made and return

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

Define a stack frame !

A

A stack frame is a data structure used by the runtime environment to manage method invokations it stores information about method paramters, method local variables, and execution context

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

Define a call stack !

A

The call stack, often simply referred to as “the stack,” is a specialized data structure used by the runtime environment of a program to manage the flow of function or method calls during program execution.

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

Give comparison between data and code in the context of stack frames !

A

When a function or method is called, its corresponding stack frame contains copies of the data associated with that function or method. This includes parameters, local variables, and other data necessary for the function or method’s execution.
Each function call creates a new instance of its stack frame, ensuring that the data within the stack frame is unique to that function call. This allows each function call to operate independently of others, with its own set of data.
Unlike data, which needs to be duplicated for each function call to ensure independence, the code itself does not need to be duplicated.
The instructions that define the behavior of functions or methods are shared among all function calls. There is only one copy of the code for each function or method, stored in memory.
Each function call references the same code, ensuring that all calls to the same function or method execute the same behavior.

Data VS Code :

Data refers to information, such as variables, parameters, and local variables, that is stored in memory and manipulated by the program during execution.
Code refers to the instructions that define the behavior of functions or methods. This includes the actual executable code written by the programmer.

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

Give an example where you call the main method of a class from the main method of another class !

A

package Koala;
public class Koala{
public static int count; // static variable
public static void main(String[] args){ // static method
System.out.println(count);
}
}

We said that the JVM basically calls Koala.main() to get the program started.
You can do this too. We can have a KoalaTester that does nothing but call the main() method.

package Koala;
public class KoalaTester{
public static void main(String[] args){
Koala.main(new String[0]); //call static method
}
}

When we run KoalaTester, it makes a call to the main() method of Koala, which prints the value of count. The purpose of all these examples is to show that main() can be called just like any other static method.

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

Give the main purposes of a static method in java !

A

In addition to main() methods, static methods have two main purposes :

For utility or helper methods that don’t require any object state. Since there
is no need to access instance variables, having static methods eliminates the
need for the caller to instantiate the object just to call the method.

For state that is shared by all instances of a class, like a counter. All instances must share the same state. Methods that only use that state should be static as well.

Utility or Helper method :
These are methods that perform specific tasks or operations related to the functionality of a class but do not necessarily rely on the state of any individual object (instance) of that class.

EXAMPLE :
The count variable represents shared state, as it is static and shared among all instances of a given Counter class.
The increment() and decrement() methods modify the shared state by incrementing and decrementing the count variable, respectively.
Since they only modify the shared state and do not require instance-specific data, they are made static.
The getCount() method only retrieves the current value of the counter. Since it only uses the shared state, it is also made static.

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