Why this compile error?

Write here if you have problems with your Java source code

Moderator: Board moderators

Post Reply
User avatar
ImLazy
Experienced poster
Posts: 215
Joined: Sat Jul 10, 2004 4:31 pm
Location: Shanghai, China

Why this compile error?

Post by ImLazy » Thu May 04, 2006 4:45 pm

Code: Select all

public class Test {
    private class A {
        public int i;
        public A() {
            i = 0;
        }
    }

    private static A x = new A();
}
When the field x is not static, it is compiled O.K. But I want the field x to be "private static". How should I deal with it?
I stay home. Don't call me out.

Darko
Guru
Posts: 580
Joined: Fri Nov 11, 2005 9:34 am
Location: Calgary, Canada

Post by Darko » Thu May 04, 2006 9:20 pm

I am not sure what you are trying to do there. Why inner class? And why static? Static variable that is an instance of something? It is doable, but - what is the benefit of it?

If you make class A static, then you can declare x static. I am not quite sure what that would do, but I guess you can do it.

And - wouldn't then all the fields of T accessed by A have to be static, too? Meaning - you wouldn't be able to access instance variables of T from A. But then, why would you have an instance in the first place, if everything is static? Well, maybe some performance boost somewhere, but I am not sure that it's worth it?

Darko

User avatar
ImLazy
Experienced poster
Posts: 215
Joined: Sat Jul 10, 2004 4:31 pm
Location: Shanghai, China

Post by ImLazy » Fri May 05, 2006 1:25 am

In fact, this is my true code:

Code: Select all

import java.util.TreeSet;

public class Base {
    private static class KeywordSet extends TreeSet<String> {
        public KeywordSet() {
            super();
            super.add("for");
            super.add("while");
            super.add("switch");
            super.add("class");
            //etc.
        }
    }
    
    private static KeywordSet keywords = new KeywordSet();
    
    public static boolean isKeyword(String key) {
        return keywords.contains(key);
    }
}
My class "Base" provides a static method "isKeyword()" which need a static TreeSet to store the keywords. So I write the class "KeywordSet" inheriting from "TreeSet" and add elements in its constructor.
But I think the class "KeywordSet" is not a component of my program logically, so I don't want other classes see this class, and so I write it inside the class "Base".

Is my way to do this right?
I stay home. Don't call me out.

Darko
Guru
Posts: 580
Joined: Fri Nov 11, 2005 9:34 am
Location: Calgary, Canada

Post by Darko » Fri May 05, 2006 3:44 am

I don't know, probably not the right person to answer this, but...

What is TreeSet lacking that KeywordSet provides? Why not just make a static TreeSet variable? If you are using a Set, I am guessing that the list will be dynamic or something? It shouldn't be static in that case, I think. I also think that TreeSet has what you need.

And - for that variable to be invisible, making it private is enough, you provide isKeyword() to the rest of the world and that is it.

I think that inner classes are there so you can have disposable objects (I don't know, I never use them except in GUI where it's convenient). That one is not going anywhere.

Darko

User avatar
ImLazy
Experienced poster
Posts: 215
Joined: Sat Jul 10, 2004 4:31 pm
Location: Shanghai, China

Post by ImLazy » Fri May 05, 2006 9:30 am

I think pure TreeSet is not suitable here, because I want to provide a static method "isKeyword()" which needs a static TreeSet. But where can I insert elements into the TreeSet? I must override the constructor of TreeSet. So I wrote a class to inherit it.
I stay home. Don't call me out.

Darko
Guru
Posts: 580
Joined: Fri Nov 11, 2005 9:34 am
Location: Calgary, Canada

Post by Darko » Fri May 05, 2006 5:12 pm

You probably want something like this:

Code: Select all

import java.util.TreeSet;

public class Base {

	private static TreeSet<String> keywords;

	static {
		keywords = new TreeSet<String>();
		keywords.add("for");
		keywords.add("while");
		keywords.add("switch");
		keywords.add("class");
// or load it from a file or whatever
	}

	public static boolean isKeyword(String key) {
		return keywords.contains(key);
	}
}

User avatar
ImLazy
Experienced poster
Posts: 215
Joined: Sat Jul 10, 2004 4:31 pm
Location: Shanghai, China

Post by ImLazy » Sat May 06, 2006 4:55 am

Cool code! That's just what I want.
Sorry, I didn't know "static" can be used in this way.
Thank you very much.
I stay home. Don't call me out.

Post Reply

Return to “Java”