[Swift vs Java] Let's understand static and final

Static

It is a member of static Class and can be attached to variables, methods, blocks, and nested classes. (Roughly variable and method)

With the static keyword ** you can access static members inside a class without an instance **. Also, no matter how many instances you create, all instances will share the same values and methods.

The'static'method does not belong to the instance, so it cannot access the members of the instance. 'static' members can only be accessed from the'static' method.

When do you use it?

Use static when it doesn't matter whether the class is created or not. For example, if you have a name and school name variable in your Student class, Since the name is unique, it is an instance variable and the school name is a static variable because it is independent without the student. The method is the same, and it feels like keeping methods that are not related to students static.

What's useful is that ** changing static in a class changes the static values of all objects that reference the class! ** **

Java

Main.java


public static void main(String args[]){
     Student s1 = new Student("Yagami");
     Student s2 = new Student("Dekisugi");

     s1.display();
     s2.display();

     System.out.println(Student.collegeName);
}

Student.java


public class Student {

        String name;
        static String collegeName ="Tokyo University";

        Student(String newStudentName){
            name = newStudentName;
        }
        void display (){
            System.out.println(name+" "+collegeName);
        }
}

Swift

Main.swift


let s1 = Student("Yagami")
let s2 = Student("Dekisugi")

s1.display()
s2.display()

print(Student.collegeName)

Student.swift


public class Student {
    
    var name:String?
    static var collegeName:String?
    
    init(_ name:String) {
        self.name = name
    }
    
    func display() {
        print("\(name) \(Student.collegeName)")
    }
}

** Point 1 ** Java's Swift is about the same, but The only difference is how to access static variables.

--In Swift, whether you're in a class or not, you can access static members Start with Student.collegeName and the class name

--In Java, you don't have to put the class name at the beginning when you are in a class

** Point 2 ** Looking at the above, the school name of the two students is "Tokyo University" even though it is not set. And since static variables aren't even referenced by the class, they aren't even used in the constructor!

Permissions for static variables and methods in a class

Java

Prorammer.java



public class Programmer {

    private static boolean isGood;
    private int experienceYears;

    public static void main(){

        //Static variables can be accessed from static methods
        isGood = true;

        //* From static method to non-Static variables are inaccessible. You have to use a reference
        Programmer person1 = new Programmer();
        person1.experienceYears = 10;
    }

    // non-Static variables can be accessed from static methods
    public void setExperienceYears(int years){
        this.experienceYears = experienceYears;
    }

    // non-non from static method-Static variables are accessible
    public void setIsGood(Boolean isGood){
        isGood = isGood;
    }
}

Swift

.swift


class Programmer{
    
    static var isGood:Bool?
    var experienceYears:Int?
    
    
    static func execute(){
        isGood = true
        
        //non from static method-Static variables are inaccessible. Requires instance
        let person1 = Programmer();
        person1.experienceYears = 10;
        
    }
    
    //non-Can be accessed from static methods
    func setExperience(_ years:Int){
        self.experienceYears = years
    }
    
    //non-Static variables cannot be accessed directly from static methods. Class name required.
    func setIsGood(_ isGood:Bool){
        Programmer.isGood = isGood
    }
}

Final

Each feature when Final is attached. Only the meaning of the variable changes Java

Final Class Final Variable Final Method
no child constant no override

Swift

Final Class Final Variable Final Method
no child no override in subclass no override

Final Class

Java

//This is OK
final public class Final extends Queen { }

//This is NG=compile error
public class King extends Final { }

Swift

.swift


//OK
final class Final: Programmer { }

//NG =compile error
final class Student: Final { }

Final Variable

Java Since it becomes Constant, the value cannot be changed. It's a let in Swift.

Swift

.swift


//You can change the value of the final variable in the OK class.
class Final{
    final var name = "Ayako Sayama"
    init() { name = "Ayako Superman" }
}

//NG Compile error
class Student: Final{
    override var name = "new name"
}

Final Method

Recommended Posts

[Swift vs Java] Let's understand static and final
JAVA learning history final modifier and static modifier
[Java] Difference between static final and final in member variables
[Java] Stack area and static area
About Java static and non-static methods
Java 15 implementation and VS Code preferences
[Java] How to use static modifiers (What are static final and static import)
Difference between final and Immutable in Java
Java static
Let's try WebSocket with Java and javascript!
A Java engineer compared Swift, Kotlin, and Java.
[Java / Swift] Comparison of Java Interface and Swift Protocol
Java programming (static clauses and "class variables")
[Java] Understand the difference between List and Set
Java and Swift comparison (1) Source control / Scope / Variables
Dynamically change private static final fields in Java
Check static and public behavior in Java methods
[Java] Understand in 10 minutes! Associative array and HashMap
Java and Swift comparison (3) Class implementation / Class inheritance / Class design
[Java] Difference between "final variable" and "immutable object"
Let's study Java
Java and JavaScript
Let's understand closures!
XXE and Java
Understand java constructor
java final modifier
Understand bugs by implementing and analyzing them (1) Deadlock (Java)
Java classes and instances to understand in the figure