Jump to content
  • Advertisement
Sign in to follow this  
hisDudeness

more identifer errors

This topic is 4507 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

[java] Here is the code:
class Widget
{
    static byte RED = 1;
    static byte WHITE = 2;
    static byte BLUE = 3;

    byte foo;

    Widget()
    {
        foo = -1;
    }

    Widget(byte f)
    {
        foo = f;
    }
}

The purpose of the three static bytes (RED, WHITE and BLUE) is to act as "fields" or constant values that will only be generated once regardless of how many Widgets I create. For instance, here is a possible usage for the code above:
// inside some other method...
Widget w = new Widget(Widget.RED);

if(w.foo == Widget.RED) callFunction1();
else if(w.foo == Widget.BLUE) classFunction2();
else callFunction3();

I am getting three identifer expected errors for each of the three lines inside of "Widget" where I declared the static bytes. What's going on here?

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
Is the method you are calling the static fields in the same package? You didn't specify an access modifier, that means the level of access used will be the default.

The default only allows classes in the same package to use it.

Share this post


Link to post
Share on other sites
Yep, the above has it correct. See java.sun.com for the default identifier access. With no identifier specified the World is not allowed to access the specified resources.

Add public in front of the declarations.

Share this post


Link to post
Share on other sites
Yes, I guess they are in the same package. I have my Widget class in the same .java file as my JApplet class. So to be more precise, the code looks like:


class Widget { /* blah */ }

class MyApplet extends JApplet
{
public void init()
{
Widget w = new Widget(Widget.RED);
}
}



I don't see why MyApplet wouldn't have access to Widget's static RED member...

Share this post


Link to post
Share on other sites
Quote:
Original post by hisDudeness
I don't see why MyApplet wouldn't have access to Widget's static RED member...

Did you try declaring them as public?

If you want different behavior for different colors, you should define an interface with an operation and have three classes implement that interface with three specific methods. This way, you do not need the if/else anymore, and adding more colors is a trivial task.

public interface ColorBehavior
{
public void doSomething();
}

public class Red implements ColorBehavior
{
public void doSomething()
{
// implementation for Red goes here
}
}

public class White implements ColorBehavior
{
public void doSomething()
{
// implementation for White goes here
}
}

public class Blue implements ColorBehavior
{
public void doSomething()
{
// implementation for Blue goes here
}
}

public class Widget
{
private ColorBehavior _behavior;

public Widget(ColorBehavior behavior)
{
_behavior = behavior;
}

public void someOtherMethod()
{
_behavior.doSomething();
}
}



This is also known as the "Strategy Pattern". If you find this stuff interesting, I recommend buying a book about "Design Patterns".

If the color classes need access to your Widget class, you can realise them as inner classes.

Share this post


Link to post
Share on other sites
Fred304,

That is an excellent suggestion, but I'm sort of stuck with the design I already have going...do you think I could clear up the indentifer expected errors if I just declare them as public (keeping the design I am using)?

Also, on an unrelated problem, I am trying to get the mathematical symbol of PI to show up as the label on a JButton. I looked up PI's unicode value and it is apprently: "1D6D1". So here is my code:

JButton pi = new JButton("0x1d6D1");

But when the applet executes the label literally reads "0x1D6D1", not the symbol for PI...

Share this post


Link to post
Share on other sites
Quote:
Original post by hisDudeness
I'm sort of stuck with the design I already have going...

Then you should buy a book about refactoring ;)

Quote:

do you think I could clear up the indentifer expected errors if I just declare them as public (keeping the design I am using)?

Sure, just use this:

public static final byte RED = 1, WHITE = 2, BLUE = 3;

I added the final keyword so nobody messes with your constants ;)

BTW you should take a look at typesafe enumerations which were introduced in Java 1.5

Can't help you with the unicode, sorry.

Share this post


Link to post
Share on other sites
Fred304 - I'm still getting the identifer expected errors.

Should I be declaring the public static bytes as members, and then initializing them in the constructors?? That wouldn't make sense though, because then I would have to assign the same values to them in each constructor, which would get pretty tiresome...

Here is my code again, just for reiteration:


class Widget
{
public static byte RED = 1, WHITE = 2, BLUE = 3;

byte foo;

// ... blah
}

public class MyApplet extends JApplet
{
public void init() { Widget w = new Widget(Widget.WHITE); }
}

Share this post


Link to post
Share on other sites
I tried your original example and did not get any errors. Must be something else, dunno...

This is the code I tried:


class Widget
{
static byte RED = 1;
static byte WHITE = 2;
static byte BLUE = 3;

byte foo;

Widget(byte x)
{
foo = x;
}
}




public class Hans
{
public static void main(String[] args)
{
Widget x = new Widget(Widget.BLUE);
}
}



No errors for me!

Share this post


Link to post
Share on other sites
Wait a minute...

Quote:

I have my Widget class in the same .java file as my JApplet class.


You can't have multiple classes in the same .java file!

Foo.java contains a class named Foo and compiles to Foo.class
Bar.java contains a class named Bar and compiles to Bar.class

etc. Use different files for different classes and you should be fine.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!