Let's summarize the Java 8 grammar from the perspective of an iOS engineer

Declaration of class in Java

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;
}

Define a class

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";
    }
}

About modifiers that can be specified for classes

Modifier How to use
public Can be referenced from any other class
unspecified Can be referenced from classes in the same package

Qualifiers that can be specified for fields and methods

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

Other commonly used modifiers

static modifier

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());
    }
}

final modifier

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.

References

[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

Let's summarize the Java 8 grammar from the perspective of an iOS engineer
How to write Scala from the perspective of Java
Java language from the perspective of Kotlin and C #
Compare the elements of an array (Java)
Ruby from the perspective of other languages
I translated the grammar of R and Java [Updated from time to time]
Become an iOS engineer
Summarize the additional elements of the Optional class in Java 9
I will explain the difference between Android application development and iOS application development from the perspective of iOS engineers
I tried to summarize the basics of kotlin and java
Get to the abbreviations from 5 examples of iterating Java lists
I tried to summarize the basic grammar of Ruby briefly
Let's summarize how to extend the expiration date of Rails
[Java] Delete the specified number of characters from the end of StringBuilder
Take a look at Kotlin from an Effective Java perspective
Generate Stream from an array of primitive types in Java
About an instance of java
From fledgling Java (3 years) to Node.js (4 years). And the impression of returning to Java
I tried to summarize the methods of Java String and StringBuilder
Let's create a TODO application in Java 5 Switch the display of TODO
How to get the length of an audio file in java
[Java] Delete the elements of List
Let's summarize Docker in an atmosphere
[Java version] The story of serialization
The road from JavaScript to Java
The origin of Java lambda expressions
[Java Servlet] The road of Senri is also the fifth step from the first step
Let's express the result of analyzing Java bytecode with a class diagram
[Payment of work] Summarize the mistakes made in 2020 [2nd year from inexperience]
The story of acquiring Java Silver in two months from completely inexperienced.
[Java Servlet] The road of Senri is also the third step from the first step
Comparison of nullable, non-nullable, non-nullable and safe types from the perspective of null safety
From Java9, the constructor of the class corresponding to primitive types is deprecated.
[Java Servlet] The road of Senri is also the fourth step from the first step
[Java] Get MimeType from the contents of the file with Apathce Tika [Kotlin]