The Artima Developer Community
Sponsored Link

Java Buzz Forum
Java Generics Question

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Weiqi Gao

Posts: 1808
Nickname: weiqigao
Registered: Jun, 2003

Weiqi Gao is a Java programmer.
Java Generics Question Posted: Jul 27, 2004 11:00 PM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Weiqi Gao.
Original Post: Java Generics Question
Feed Title: Weiqi Gao's Weblog
Feed URL: http://www.weiqigao.com/blog/rss.xml
Feed Description: Sharing My Experience...
Latest Java Buzz Posts
Latest Java Buzz Posts by Weiqi Gao
Latest Posts From Weiqi Gao's Weblog

Advertisement

In my quest to develop a generic singleton class, I encountered this problem:

[weiqi@gao] $ cat Singleton.java
public class Singleton<T> {
  private static T instance;
  public static T getInstance() {
    if (instance == null) {
      instance = new T();
    }
    return instance;
  }
}
[weiqi@gao] $ javac Singleton.java
Singleton.java:2: non-static class T cannot be referenced from a static context
    private static T instance;
                   ^
Singleton.java:3: non-static class T cannot be referenced from a static context
    public static T getInstance() {
                  ^
Singleton.java:5: non-static class T cannot be referenced from a static context
            instance = new T();
                           ^
3 errors

Why? (Both C++ and C# allow similar constructs.)

Read: Java Generics Question

Topic: Struts 1.2.1 MappingDispatchAction Previous Topic   Next Topic Topic: cvs2svn: brilliant!

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use