02 Prelim CP2 (continuation) Flashcards
The (1)_____ keyword can be used to allow
declaration of static variables and
methods that belong to the (2)_____ instead of a (3)______ instance (such as the main method):
- static
- Class
- Specific
What is the static variable in this code:
public class StaticExample {
static int staticNumber = 10; static void staticMethod() { System.out.println("Static number is: " + staticNumber) } public static void main (String[] args) { StaticExample.staticMethod(); System.out.print(staticNumber): } }
staticNumber
What is the method in this code:
public class StaticExample {
static int staticNumber = 10; static void staticMethod() { System.out.println("Static number is: " + staticNumber) } public static void main (String[] args) { StaticExample.staticMethod(); System.out.print(staticNumber): }
}
staticMethod()
What is the output of this code:
public class StaticExample {
static int staticNumber = 10; static void staticMethod() { System.out.println("Static number is: " + staticNumber) } public static void main (String[] args) { StaticExample.staticMethod(); System.out.print(staticNumber): }
}
Output:
Static number is: 10
10
(1)_______ refers to the process of creating an actual object (or instance) from a class. In object-oriented programming, a class is like a blueprint, and
(2)______ the class means creating an object based on that blueprint.
- Instantiation
- Instantiating
instantiating the class means….
creating an object based on that blueprint.
(1)________Instantiation allows you to reuse the same class multiple times to create different objects with potentially different states. This reduces code (2)_______.
- Reusability
- Redundancy
What is the output:
public class InstanceExample {
// Instance variable (not static) int instanceNumber = 10; // Instance method (not static) static void InstanceMethod() { System.out.println("Instance number is: " + instanceNumber) } public static void main (String[] args) { // Create an object (instatiate the class) instanceExample example = new instanceExample(); // call the instance method using the object example.instanceMethod(): } }
Output:
Instance number is: 10
What is the output:
public class sample {
static int count = 0 ; // shared by all public void increment () { count ++; // increment shared variable } public static void main (String[] args) { sample A = new Sample(); // first instance sample B = new Sample(); // second instance A.increment() // count becomes 1 System.out.println("A.count"); System.out.println("Object A: " + A.count ); // prints 1 B.increment() // count becomes 2 System.out.println("Object B: " + B.count ); //prints 2 System.out.println("Object A: " + A.count ); // prints 2 } }
A.count
Object A: 1
Object B: 2
Object A: 1
For example, we could define a method named sum that returns the sum of its two (2) parameters:
Syntax:
static int sum(int val1, int val2) {
return val1 + val 2 ;
}
Return Type
Return Method (Return type)
What is the output:
class Myclass {
static int sum(int val1, int val2) {
return val1 + val2;
}
public static void main(String[ ] args) { int x = sum(2,5); System.out.println(x); } }
Output: 7
What keyword should you use if you do not want your method to return anything?
Void
Return Method Outputs:
// Sample 1
static int returnFive()
return 5;
// Sample 2 (has a parameter) {
static void sayHelloTo(String name)
System.out.println(“Hello”+ name)
}
Use john
// Sample 3
static void sayHello() {
system.out.println(“Hello world!”);
}
S1: 5
S2: Hello John
S3: Hello World!
Fill in the blanks to declare an integer and pass it as a parameter to the test() method:
class MyClass {
public static void main(String [] args) {
____ x = 10;
test(__);
}
static void test(int x) { System.out.println(x); } }
int x = 10;
test(x) ;
______ are basically variables within a class:
Attributes
Access Modifiers
- default
- public
- protected
- private
Access Modifier
public static void main(Stringl ] args)
______ is an access modifier, it is used to set the levels of access.
public
______ a variable or method declared with no access control modifier is available to any other class in the same package
default
□ _______ - accessible from any other class.
public
□ _______ - provides the same access as the default access modifier, with the addition that subclasses can access protected methods and variables of the superclass.
protected
□ _______ - accessible only within the declared class itself.
private
______ and ______ are used to effectively
protect your data when creating classes.
Getters
Setters
> The _____ _____ returns the value of the
attribute.
Getter method
> The _____ ____ takes a parameter and
assigns it to the attribute.
Setter method
Getters start with (1)______, followed by the variable name, with the first letter of the variable name (2)_____.
Setters start with (3)___, followed by the variable name, with the first letter of the variable name (2)_______.
- get
- Capitalized
- set