473,387 Members | 1,517 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Singleton

oll3i
679 512MB
Expand|Select|Wrap|Line Numbers
  1. public class Cennik {
  2.      private static Cennik instance = null;
  3.  
  4.        private Cennik() {  // prywatny konstruktor
  5.        }
  6.  
  7.        public static Cennik getInstance() {
  8.           if(instance == null) {
  9.              instance = new Cennik();
  10.           }
  11.           return instance;
  12.        }
  13.  
  14. }
  15.  
  16.  
  17. public class Kwiaciarnia {
  18.   Cennik c= Cennik.getInstance();
  19. }
  20.  
but eclipse highlights getInstance(); and says the method getInstance() is undefined for type Cennik
Mar 28 '07 #1
5 1594
sicarie
4,677 Expert Mod 4TB
Expand|Select|Wrap|Line Numbers
  1. public class Cennik {
  2.      private static Cennik instance = null;
  3.  
  4.        private Cennik() {  // prywatny konstruktor
  5.        }
  6.  
  7.        public static Cennik getInstance() {
  8.           if(instance == null) {
  9.              instance = new Cennik();
  10.           }
  11.           return instance;
  12.        }
  13.  
  14. }
  15.  
  16.  
  17. public class Kwiaciarnia {
  18.   Cennik c= Cennik.getInstance();
  19. }
  20.  
but eclipse highlights getInstance(); and says the method getInstance() is undefined for type Cennik
I don't think you want to set your declaration of instance to null (private static Cennik instance = null; ). Other than that, I think that the only other thing you're missing is the copy constructor - someone with malicious intentions can overload the default copy constructor and create a copy of a singleton object, having two at the same time, you want to have something like

Expand|Select|Wrap|Line Numbers
  1. clone() throws CloneNotSupportedException {
  2.     throw new CloneNotSupportedException;
  3. }
  4.  
Mar 28 '07 #2
oll3i
679 512MB
sorry but where
Mar 29 '07 #3
sicarie
4,677 Expert Mod 4TB
sorry but where
Just as a method in your Cennik class.
Mar 30 '07 #4
RedSon
5,000 Expert 4TB
Since instance is static doesn't that mean that it cannot get a "new" object during runtime. I thought the point of static ment that it cannot be changed except at build time.
Mar 30 '07 #5
sicarie
4,677 Expert Mod 4TB
Since instance is static doesn't that mean that it cannot get a "new" object during runtime. I thought the point of static ment that it cannot be changed except at build time.
Honestly, I have no idea. I learned 'static' when I was programming in C++, and it's completely different in Java (I thought it had something to do with access, but I gave up on trying to get the finer points of Java like the meaning of static and how Java does memory management.
Mar 30 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Tim Clacy | last post by:
Is there such a thing as a Singleton template that actually saves programming effort? Is it possible to actually use a template to make an arbitrary class a singleton without having to: a)...
10
by: E. Robert Tisdale | last post by:
Could somebody please help me with the definition of a singleton? > cat singleton.cc class { private: // representation int A; int B; public: //functions
1
by: Jim Strathmeyer | last post by:
So I'm trying to implement a singleton template class, but I'm getting a confusing 'undefined reference' when it tries to link. Here's the code and g++'s output. Any help? // singleton.h ...
3
by: Alicia Roberts | last post by:
Hello everyone, I have been researching the Singleton Pattern. Since the singleton pattern uses a private constructor which in turn reduces extendability, if you make the Singleton Polymorphic...
7
by: Ethan | last post by:
Hi, I have a class defined as a "Singleton" (Design Pattern). The codes are attached below. My questions are: 1. Does it has mem leak? If no, when did the destructor called? If yes, how can I...
3
by: Harry | last post by:
Hi ppl I have a doubt on singleton class. I am writing a program below class singleton { private: singleton(){}; public: //way 1
5
by: Pelle Beckman | last post by:
Hi, I've done some progress in writing a rather simple singleton template. However, I need a smart way to pass constructor arguments via the template. I've been suggested reading "Modern C++...
6
by: Manuel | last post by:
Consider the classic singleton (from Thinking in C++): ----------------------------------------------------- //: C10:SingletonPattern.cpp #include <iostream> using namespace std; class...
3
weaknessforcats
by: weaknessforcats | last post by:
Design Pattern: The Singleton Overview Use the Singleton Design Pattern when you want to have only one instance of a class. This single instance must have a single global point of access. That...
3
by: stevewilliams2004 | last post by:
I am attempting to create a singleton, and was wondering if someone could give me a sanity check on the design - does it accomplish my constraints, and/or am I over complicating things. My design...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.