Java static instance -
so started fist big project in java , i'm following tutorial there code dont understand @ all.
package com.legolando.runa; import net.minecraftforge.fml.common.mod; @mod(modid = reference.modid, name = reference.modname, version = reference.version) public class runa { @mod.instance public static runa instance = new runa(); // dont why instance of class has static }
as see create instance of class inside class (already cosmos me) , instance static. can explain static instance? same static variable or method?
this code reminds me of singleton class in java.
public class runa { private static runa singleton = new runa( ); /* private constructor prevents other * class instantiating. */ private runa() { } /* static 'instance' method */ public static runa getinstance( ) { return singleton; } /* other methods protected singleton-ness */ protected static void demomethod( ) { system.out.println("demomethod singleton"); } }
fyi, singleton ensures 1 object of created class runa inside application. try google bit more understanding of singleton usage
in java
links: https://www.tutorialspoint.com/java/java_using_singleton.htm
best of luck
Well explained . Great article on OOP . There is also good java resource for beginners visit Java Tutorials
ReplyDelete