S
S
Sergey Nalomenko2012-02-04 02:09:55
Java
Sergey Nalomenko, 2012-02-04 02:09:55

Same name variables and classes in Java (J2ME)?

Hi all!
Faced this problem: static String class variable has the same name as another class in my default package. And now when I try to call any static class method, NetBeans gives me an error.

preambula = (<b>decoder</b>.go(s = new String(reader.get_contents(&quot;/info.txt&quot;, false), &quot;UTF-8&quot;), ';'))[0];<br/>
 <b>decoder </b>= &quot;select&quot;;<br/>

As you can see, in the first line decoder is the name of the class located in the same package, and in the second line is a string variable. How do I fix the problem so that NetBeans doesn't think that on the first line I'm trying to access a string variable method?
PS: Suggestions to use a refractor are not accepted ;)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
anmipo, 2012-02-04
@anmipo

If you followed the basic rules of Java ( code conventions ), this problem would not arise at all.
First, class names must begin with a capital letter (and method names must be in camelCase, no underscores).
Second, using the default package is considered bad form ; What's stopping you from giving the package a name?

E
esin, 2012-02-04
@esin

Messed up with classes. The above example is a bit clumsy. Here is the normal one:

public class TestClass 
{
  public TestClass()
  {

  }

  public String getStringPublic()
  {
    return "1234";
  }

  public static void doNothing()
  {
    //
  }

  public static String getString()
  {
    return "123";
  }
}



package test;

public class Main
{
  public static TestClass globalTestClass = new TestClass();
  
  @SuppressWarnings("static-access")
  public static void main(String[] args)
  {
    globalTestClass.doNothing();
    globalTestClass.getString();
    globalTestClass.getStringPublic();
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question