Java in , If we create a class , I want this class to have only one object , Then we can

1: The construction method of this class is designed as private

2: Define a static Method , Create objects in this method

package test;
/*
* Sometimes we want a class to have only one instance at any time , At this time, we can design this class as singleton pattern .
* To design a class as a singleton pattern , need :
* 1: The access modifier of the constructor of this class is private
* 2: Then define a static Method , Create the object of the class in this method
*
* */
public class Singleton {
private static final Singleton INSTANCE = new Singleton(); // Private member
private int a = 0;// Private member private Singleton(){}// Construction method
public static synchronized Singleton getInstance(){// Class method
return INSTANCE;
}
public void methodA(){ // Member method
a++;
System.out.println("a = "+a);
}
public static void main(String[] args){ // Main method
Singleton sg1 = Singleton.getInstance();
Singleton sg2 = Singleton.getInstance();
sg1.methodA();
sg2.methodA();
System.out.println(sg1 == sg2);
} }

java There can only be one instance of the class creation in

  1. Spring Boot There can only be one of them WebMvcConfigurationSupport Configuration class

    First, write the conclusion at the front of the article , There can only be one inheritance in a project WebMvcConfigurationSupport Of @Configuration class ( Use @EnableMvc The effect is the same ), If there are more than one such class , Only one ...

  2. Java When instantiating a class in , There is no initial value in this class int Type member variable i,i Is the value of 0?

    java There are two types of numerical value , The other is that the initial value of class variable is 0, Class variables are initialized to null No initialization of member variables int The sex variable is 0, stay java There's a rule in the book , Variables declared in methods must be initialized when used ...

  3. Java The program only runs one instance [ turn ]

    If you want your Java There can only be one instance of the program , Please refer to the usage below . Link to the original text :http://blog.csdn.net/yaerfeng/article/details/7264729 Java There is no provision for this ...

  4. On why a java There can only be one... In the source file public class ?

    Statement , This article is a reprint of Reprint http://blog.csdn.net/bareheadzzq/article/details/6562211 Recently in a java Several classes are implemented in the file , One of the statements is pub ...

  5. Why one java There can only be one... In the source file public class

    problem : One ".java" Can multiple classes be included in the source file ( Not an inner class )? What are the limitations ? answer : There can be multiple classes , But only one public Class , also public The class name of must match the file name . A file ...

  6. Oracle Remote access configuration stay Windows Forms and WPF Use in application FontAwesome Icon C# Deserialization XML abnormal : stay XML file (0, 0) There is a mistake in “ Missing root element ” C#[Win32&WinCE&WM] An application can only run one instance :MutexHelper Decimal Type interception reservation N Place the decimal up to , Decimal Type interception reservation N Place decimals without rounding

    Oracle Remote access configuration   Server configuration If you don't want to write it yourself , Can pass  Net Manager  To configure the . In the following configuration file  localhost  Change it to  ip  Address , otherwise , Remote cannot access . 1. Network monitoring configuration ...

  7. Java Directly output the object of a class in

    for example package com.atguigu.java.fanshe; public class Person { String name; private int age; public Strin ...

  8. Java How to view a package that a class depends on in

    Java How to view a package that a class depends on in Pictured , How do I know JSONArray Which package does it depend on , There are two json-lib package ?   Test statement :   public static void main(Strin ...

  9. Java The difference between static variables and instance variables in

    Java The difference between static variables and instance variables in Static variables are classes , This class does not produce objects , Static variables can be called through the class name . The object whose instance variable belongs to this class , You have to generate this kind of object , To call instance variables . Differences in program runtime : Instance variables belong to a pair ...

Random recommendation

  1. aliyun source.list

    The telecommunication network is becoming less and less popular .ubuntu The environment uses the following source.list deb http://mirrors.aliyun.com/ubuntu/ trusty main restricted un ...

  2. C# abstract class

    Abstract class use abstract Modifier declaration : Cannot create an instance of an abstract class : Abstract classes can only be used as base classes for other classes : Abstract classes can contain abstract members and ordinary non abstract members : An abstract class itself can derive from another abstract class : Anything derived from an abstract class [ class ...

  3. NPOI course

    NPOI yes POI Project .NET edition .POI It's an open source Java Reading and writing Excel.WORD Wait for Microsoft OLE2 Items for component documents . NPOI Official website :http://npoi.codeplex.com/( ...

  4. 【bzoj2002】[Hnoi2010]Bounce Flying sheep Block

    [bzoj2002][Hnoi2010]Bounce Flying sheep 2014 year 7 month 30 Japan 8101 Description One day ,Lostmonkey Invented a super elastic device , To show off in front of his sheep friends , He invited ...

  5. vsftp Black and white list settings and problems

    Question 1 :ftpusers and user_list What is the purpose of each of the two documents ? What does it matter ? First of all, please be clear :ftpusers Not affected by any compounding items , It always works , It's a blacklist ! The file is stored in a forbidden access FTP The use of ...

  6. ( Reprint )Cocos2dx-OpenGL ES2.0 course : First time to know MVP(3)

    In the last article , I'm introducing vertex shader I dug a hole when I was in bed :CC_MVPMatrix. It's actually a uniform, every last cocos2d-x Predefined shader They all contain this uniform, however ...

  7. Use awrextr.sql export awr Raw data

    1.AWR Raw data and AWR The difference between the reports AWR Raw data : yes oracle database mmon The process periodically dumps statistics from memory to disk , And in a structured form stored in a number of tables to form their own active workload storage warehouse (AutomaticWor ...

  8. Java Basics —ClassLoader The understanding of the ( turn )

    The three default class loaders Java The default is three ClassLoader, According to the hierarchy, from top to bottom is : Bootstrap ClassLoader Ext ClassLoader System ClassLoad ...

  9. java web system optimization , mysql Query optimization

    1. Using database connection pool technology 2. Optimize sql Sentence format , For example, use PreparedStatement Instead of Statement, The former avoids repeated compilation , The latter needs to parse and compile the database every time , Reduce the efficiency of database access ...

  10. enhance for Loop assignment

    enhance for Loop assignment The code is as follows : double[] testList01 = new double[5]; java.util.Scanner sc = new java.util.Scanner(Sy ...