Student.java
class Student {
//Field declaration
//Declare name of type String
String name;
//Declare int type score
int score;
//Declare a class constant of type int
static final int MAX_SCORE = 100;
}
class Takashi {
//The static modifier is the class name.Can be accessed by member name
int mathScore = Student.MAX_SCORE;
}
SampleClass.java
public class SampleClass { //name of the class
private String name = "Sample"; //Field name(iOS:Property)
public String action() { //Method name
return name + "> " + "Action";
}
}
Modifier | How to use |
---|---|
public | Can be referenced from any other class |
unspecified | Can be referenced from classes in the same package |
Modifier | How to use |
---|---|
public | Can be referenced from any other class |
protected | Can be referenced from child classes and classes in the same package |
unspecified | Can be referenced from classes in the same package |
private | Only accessible inside your class |
The static modifier can define methods and fields that can be called without creating an instance.
Fields and methods with the static modifier are called class members
and can be called without creating an instance.
How to use class members can be called by "class name.class member".
StaticTest.java
public class StaticTest {
//Class field
static String staticField = "World";
//Class method
static String staticMethod() {
return "yes!";
}
//Instance field
String instanceField = "Hello";
//Instance method
String instanceMethod() {
return instanceField + " " + staticField + " " + staticMethod();
}
}
How to use
StaticTestMain.java
public class StaticTestMain {
public static void main(String... args) {
System.out.print(StaticTest.staticField);
System.out.print(StaticTest.staticMethod());
StaticTest.staticField = "Japan";
System.out.print(StaticTest.staticField);
StaticTest test = new StaticTest();
System.out.print(test.staticField);
System.out.print(test.staticMethod());
System.out.print(test.instanceMethod());
}
}
The final modifier is a modifier that makes a variable immutable.
Declaration method
FinalTest.java
public class FinalTest {
//Class constant
static final String GREETING_MESSAGE = "Hello";
}
How to use
StaticTestMain.java
public class StaticTestMain {
public static void main(String... args) {
System.out.print(FinalTest.GREETING_MESSAGE);
}
}
I learned a lot. I would like to study the design patterns in Android development as soon as possible.
[JAVA full-scale introduction ~ Java full-scale introduction ~ from basics by modern style to object-oriented / practical library](https://www.amazon.co.jp/Java%E6%9C%AC%E6%A0%BC%E5% 85% A5% E9% 96% 80-% E3% 83% A2% E3% 83% 80% E3% 83% B3% E3% 82% B9% E3% 82% BF% E3% 82% A4% E3% 83 % AB% E3% 81% AB% E3% 82% 88% E3% 82% 8B% E5% 9F% BA% E7% A4% 8E% E3% 81% 8B% E3% 82% 89% E3% 82% AA % E3% 83% 96% E3% 82% B8% E3% 82% A7% E3% 82% AF% E3% 83% 88% E6% 8C% 87% E5% 90% 91% E3% 83% BB% E5 % AE% 9F% E7% 94% A8% E3% 83% A9% E3% 82% A4% E3% 83% 96% E3% 83% A9% E3% 83% AA% E3% 81% BE% E3% 81 % A7-% E8% B0% B7% E6% 9C% AC-% E5% BF% 83 / dp / 477418909X / ref = sr_1_1? S = books & ie = UTF8 & qid = 1526715238 & sr = 1-1 & keywords = JAVA% E6% 9C% AC % E6% A0% BC% E5% 85% A5% E9% 96% 80)
Recommended Posts