11 [Instructor] Java Essentials ch06
| Class variable, instance variable //property
cv iv
Must be maintained differently for each object iv
Common to all objects cv //static+
Card c = new Card();
c .kind = "HEART"; //iv
c.number = 5;
Card .width = 200; //Cv class name in front, reference variables also work
Card.height = 300;
*Draw according to flash video MemberVar
| What is a method? Method declaration and implementation
Method { } - unit of work
Code duplication needs to be eliminated, easy to manage, reusable, concise
Methods must be in a class, functions must be independent
It is better to write it to perform only one function.
int add (int a, int b) // declaration part, return type int
{ //Implementation part
int result = a + b;
return result;
}
When there is nothing to return, return type void
//Parameter a, b && result → local variable lv
It doesn't matter if the names overlap, as it only applies within the method.
| Method call, method execution flow
Call: method name(); //When calling void print99danAll()
int result = add (3, 5);
class MyMath {
long add (long a, long b) { //(1)
long result = a + b;
return result;
}
long add(long a, long b) { return a + b; } //(2)
//(1) and (2) expressions are the same value
a > b ? a : b;
//If a>b, return a, otherwise b
//Simply used instead of if-else statement, ternary operator
//It can be called without a variable (result) that stores the value, but it cannot be stored in the address, only a call is possible.
1. Object creation
2. Method call
3. All method statements are executed, or return to the calling method in the return statement and execute subsequent statements.
| return statement, return value
void printGugudan(int dan) {
if(!(2
return;
for(int i=1; i
System.out.printf( "%d * %d = %d%n", dan, i, dan*i );
return; // The return type is void, so it can be omitted. Compiler automatically adds
}
//In other cases, it is necessary to write a return statement for both true and false.
//Return type must also match
| call stack call stack
In single thread, only one method is executing, others are waiting.
//Draw step by step
| primitive parameters
Basic type parameter - can only read the value of the variable
Reference parameter - allows you to read and change the value of a variable
*flash video PrimitiveParam, ReferenceParam
public static void main(String[] args) {
Date d = new Date();
d.x = 10; //The x value pointed to by the reference variable d is 10.
System.out.println("main() : x =" +d.x);
change(d.x);
System.out.println("After change(d.x)");
System.out.println("main() : x =" +d.x);
}
static void change( int x ) {
x = 1000;
System.out.println("change() : x =" +x);
}
//The x value of change and x value of Date are different
| reference parameter
public static void main(String[] args) {
Date2 d = new Date2();
d.x = 10;
System.out.println("main() : x =" +d.x);
change(d.x);
System.out.println("After change(d.x)");
System.out.println("main() : x =" +d.x);
}
static void change( Date2 d ) { //Reference type parameter, copy local variable d value of main method to change
d.x = 1000;
System.out.println("change() : x =" +d.x);
}
Reference return type
public static void main(String[] args) {
Data3 d = new Data3();
d.x = 10;
Data3 d2 = copy(d); //No need for reference variable, because it is a static method
System.out.println("d.x ="+d.x);
System.out.println("d2.x"+d2.x);
}
static Data3 copy( Data3 d ) {
Data3 tmp = new Data3();
tmp.x = d.x;
return tmp ; //Returns the address of the reference tmp object
}
//Exchanging the address of the object
*Draw a picture
| static and instant methods
1. static method = class method
Called with ‘class name.method name()’ without creating an object
Instance variables (iv) cannot be used within methods.
Math.ramdom( ), Math.round( )
2. Instant method
After creating an instance, call it with ‘reference variable.method name()’
Instance variables can be used within methods
iv, im
class MyMath2 {
long a, b; //iv instance variable, iv
long add ( ) { //Instance method, no parameters due to use of iv
return a + b;
}
static long add(long a, long b) { //class method (static method), parameters, lv
return a + b;
}
}
System.out.println(MyMath2.add(200L, 100L); //Class method call, no object (iv bundle) required
MyMath2 mm = new MyMath2 ( ); //Instance method call, object creation required
mm.a = 200;
mm.b = 100;
System.out.println(mm.add( ));
static 1. Attached to a common property 2. Attached to a method that does not use an instance member
Comments
No comments yet. Be the first!
164 posts in 테크
- 2233D Gaussian Splatting vs Unreal Engine: Two Ways to Build a 3D World — and Where Each One Ships
- 222LLMs Inside Unreal Engine: The New Skills Game Developers Need in 2026
- 220Living With Claude Fable 5: How the Most Capable Model Changes the Way You Actually Work
- 219Luma's Bet: From Video Generator to a Single Model That Thinks in Pixels
- 215The Best AI Video Models in 2026: Types, Differences, and Where This Is All Going