Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 185551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T15:29:23+00:00 2026-05-11T15:29:23+00:00

I have a Set class (This is J2ME, so I have limited access to

  • 0

I have a Set class (This is J2ME, so I have limited access to the standard API; just to explain my apparent wheel-reinvention). I am using my set class to create constant sets of things in classes and subclasses. It sort of looks like this…

class ParentClass {     protected final static Set THE_SET = new Set() {{         add('one');         add('two');         add('three');     }}; }   class SubClass extends ParentClass {     protected final static Set THE_SET = new Set() {{         add('four');         add('five');         add('six');         union(ParentClass.THE_SET); /* [1] */     }}; } 

All looks fine, except the line at [1] causes a null pointer exception. Presumably this means that the static initialiser in the subclass is being run before that of the parent class. This surprised me because I’d have thought it would run the static blocks in any new imports first, before running any in the instatiated subclass.

Am I right in this assumption? Is there any way to control or work around this behaviour?

Update:

Things are even stranger. I tried this instead (Note the ‘new ParentClass()’ line):

class ParentClass {     public ParentClass()     {         System.out.println(THE_SET);     }      protected final static Set THE_SET = new Set() {{         add('one');         add('two');         add('three');     }}; }   class SubClass extends ParentClass {     protected final static Set THE_SET = new Set() {{         System.out.println('a');         new ParentClass();         System.out.println('b');         add('four');         System.out.println('c');         add('five');         System.out.println('d');         add('six');         System.out.println('e');         union(ParentClass.THE_SET); /* [1] */         System.out.println('f');     }}; } 

And the output is strange:

a ['one', 'two', 'three'] b c d e Exception in thread 'main' java.lang.ExceptionInInitializerError Caused by: java.lang.NullPointerException 

So ParentClass is initialised, but the subclass doesn’t have access to it in its static initializer.

  • 1 1 Answer
  • 4 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T15:29:24+00:00Added an answer on May 11, 2026 at 3:29 pm

    Is this what you are trying to accomplish? Or do you need a local implementation of the Set interface?

    class ParentClass {     protected final static Set THE_SET;      static {         THE_SET = new HashSet();         THE_SET.add('one');         THE_SET.add('two');         THE_SET.add('three');     } }   class SubClass extends ParentClass {     protected final static Set THE_SECOND_SET;      static {         THE_SECOND_SET = new HashSet();         THE_SECOND_SET.add('four');         THE_SECOND_SET.add('five');         THE_SECOND_SET.add('six');         union(ParentClass.THE_SET); /* [1] */     } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this class set for a page I'm testing (just starting to try
I have something like this set up: class CategoryPage (webapp.RequestHandler): def get(self): ** DO
I have a two models set up like this: class User < ActiveRecord::Base #
I have this Class: public class User { public string id{ get; set; }
Say I have this class: class myclass { public int Field1{ get; set; }
I have a class like this, public class person { name Name {set; get;}
I have this div inside a repeater, where i set the class, onmouseover and
I have a Hibernate mapping that looks something like this: <class name=MyEntity> <set name=scalarSet
I have a class set up like this: class Foo { Foo(); private: Bar
I have set an image 32x32px as a list style image like this: .class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.