Class question
14/02/2018 12:08
Hi,
I have seeen the following in java class:

public class MyClass {
static {
try{
}catch(Exception e){
}
}
synchronized public static MyClass instance() { if (instance == null) { instance = new MyClass(); }
return instance; }
}
1. What is the interpretation of static { } ?2. What is the interpretation of 'synchronized public static MyClass instance()'?
cheers,
//mikael

Source is Usenet: comp.lang.java.help
Sign in to add a comment

Answer score: 5
14/02/2018 12:08 - Hi,
It is the static initializer which is executed when the class is loaded.


What exactly is the point you do not understand?
Ciao,Ingo

Source is Usenet: comp.lang.java.help
Sign in to add a comment

Answer score: 5
14/02/2018 12:08 - When will it be loaded?
Is this a singleton to make sure I only have one instance of this class? Why would I make it synchronized?
cheers,
//mikael

Source is Usenet: comp.lang.java.help
Sign in to add a comment

Answer score: 5
14/02/2018 12:08 - Hi,
When the it is used the first time. (Note that e.g. a simple 'import' does not mean that the class is used.)
See the langspec if you need more precise information (which should not be necessary).


The implementation suggests that it is a singleton, indeed.


But I thought you had problems with the declaration, not with the implementation?
Which of the parts do you have problems with?:
- synchronized- public- static- MyClass- instance- Braces ()
> Why would I make it synchronized?
Ever heard of multi-threading? If not, then read a good book or tutorial (e.g. see Roedys hint).


Ciao,Ingo

Source is Usenet: comp.lang.java.help
Sign in to add a comment

eDiscover
Helpforce eDiscover provides technical articles updated each dayHelpforce eDiscover RSS feed contains the latest technical articles in RSS
Click the logo to go back to the main page
Search eDiscover
  
Categories

Click an icon to go to that category

Helpforce eDiscover contains articles about Microsoft Windows Helpforce eDiscover contains articles about Apple products and MacOS Helpforce eDiscover contains articles about Linux and POSIX operating systems Helpforce eDiscover contains articles about Helpforce Helpforce has a large variety of technical information and articles for you to read Helpforce eDiscover contains articles about databases, MYSQL, SQL Server Oracle Helpforce eDiscover contains articles about Java, JVM and the JRE Helpforce eDiscover contains articles about the QNX operating system Helpforce eDiscover contains articles about Oracle Solaris and Open Solaris Helpforce eDiscover contains articles about RISC OS, Acorn and the BBC Micro Helpforce eDiscover contains articles about Amiga and AmigaOS

Type your comment into the box below