Today we delved a little bit deeper into methods and different access modifiers and its functions!
There are many keywords we can add infront of a member to define what exactly we want it to do!
Here's a handy diagram of what settings there are!
access modifiers / static , non-static/ return-type / method name(parameter... (optional)){
command;
command;
} end of method body
Static
As we touched upon it before, this keyword prevents it being a part of the class when a component is formed.
class Apple{
public static void print() {
System.out.println("Apple class에 있는 print 메소드");
};
}
Therefore we can only call a static method by calling the class directly like this: Class.staticMethod( );
public static void main(String[] args) {
Apple.print();
}
This means it can be shared throughout all of the component of the same class but not copied!
Here are some example of static being in action!
Example A:
class Apple{
static int count; //초기화 하지 않아도 초기화 됨, 초기값: 0
public Apple() {// 기본 생성자
count++;
}
}
public class Static01 {
public static void main(String[] args) {//클래스명.변수명
Apple apple01 = new Apple();
Apple apple02 = new Apple();
Apple apple03 = new Apple();
System.out.println(Apple.count); //3
}
}
Example B:
class Static {
public int a = 20;
static int b = 0;
}
public class Test01 {
public static void main(String[] args) {
int a;
a = 10;
Static.b = a;
Static st = new Static();
System.out.println(Static.b++); // 10
System.out.println(st.b); // 11
System.out.println(a); // 10
System.out.println(st.a); //20
}
}
How to decide when to use static and when to use instances
static member (also called a class member): doesn't need to be processed as an instance to be used, and can be used by all members of the same class!
instance member: it has it's own memory hub that holds the unique value of they receive when they're first formed.
Use instance if it's data that every object needs or if it contains instance fields, and use static if it's communal data or if it doesn't require instances.
Important thing to note: We can summon static members in static methods however we cannot use the this keyword since it references the instance the class contains!
Strangely static methods cannot summon non-static fields but normal methods can summon static fields!
putting an additional keyword final will turn that member into a constant and will not be altered (useful for login details or personal info that cannot be changed)
Singleton Pattern: a further use of static
class Connection { //singleton pattern
private static Connection _inst = null;
private int count = 0;
private Connection() {};
// 외부에서 Connection constructor를 불러서 생성/수정 하는걸 막는다
static public Connection get() {
if (_inst == null) {
_inst = new Connection();
return _inst;
}
return _inst;
}
public void count() {
count++;
};
public int getCount() {
return count;
}
}
public class StaticEx02 {
public static void main(String[] args) {
Connection conn1 = Connection.get();
conn1.count(); //
Connection conn2 = Connection.get();
conn2.count();
//conn1과 conn2는 같다, .get()에서 똑같은 _inst를 되돌려주기 때문
Connection conn3 = Connection.get();
conn3.count();
conn1.count();
System.out.print(conn1.getCount()); //4
}
}
using a combination of private and static members, the singleton pattern only returns one value despite having multiple members created.'
It also uses a getter/setter which is used to retrive data set as private.
When creating a getter/setter, the cursor must be inside the class before going to:
source> generate getter/setter >select elements> set to public > close
Access modifiers
In Java we can approach each API using an access modifier. Here is a list of said modifiers arranged to size:
public(open to all) > protected > default(when it is empty, when no access modifiers are used) > private
- public: covers the most ground as it can be called without the limits of packages, they can be accessed despite which package it might be located in.
- protected: Can be approached with the same package. In different packages it can only be accessed between child and parent classes and interfaces. (not used very often)
- default: no access modifiers set. Can be apporached only if in same package
- private: can only be approached within same class

Classes can only be public or default!
'JAVA' 카테고리의 다른 글
Interface and Exceptions (0) | 2024.07.02 |
---|---|
Recap of Access Modifiers and deep dive into 4 pillars of OOP (0) | 2024.07.01 |
Object Oriented Programming: Fields, Constructors and methods (0) | 2024.06.27 |
JAVA: Deep and Shallow Copying, String Class Methods (0) | 2024.06.26 |
JAVA: creating a JSP page using Apache, 2D Arrays part. 2, OOP fundamentals (0) | 2024.06.25 |