Hi,

I wonder how can access to a static method of a class called width from another object that has an attribute called width too:

ex:

public class width{
   private int test;

   public static int testValue(){
     return this.test;
   }

}

public class Rect{
   private int width;
   private int height;

   public void something(){
      width.testValue(); //This will try to access to the attribute. But what i want is to access to the class static method.
   }

}

I've tried with the Class.forName("width").testValue() but didnt work.

Thanks

First choice would be to clean up your namespace. Do both class and attribute need to have the same name? If so, using the standard capitalization will clear it up: the class would typically be called Width, not width, so there's no conflict.

Hi,

Thanks for your answer. To change the name of variable or class is impossible because im using libs from already compiled classes.

Im in the default package,
Do you have more ideas to access to a method/attribute of the static class?

Thanks

well if you have access to the Rect class then shouldn't you be able to change the variable named width to something say rect_width?
This way the name conflict should not arise.

Another option is to extend the width class with a sub-class and have a function in there that calls the testValue class.

ex

public class width2 extends width{

public static int testValue(){
  super();
  return super.test;//something like this
}
}

and then you would just call
width2.testValue();

Hi,

Thanks for the answer.

Yes, that's how im doing write now. The problem is that i have a lot of functions, but, ok that's a solution (for not final classes), and what about attributes?

Imagine that instead the testValue is a method, is in fact an attribute. Ex:

public class width{
   public static int testValue;
}

public class Rect{
   private int width;
   private int height;

   public void something(){
      width.testValue; //This will try to access to the attribute. But what i want is to access to the class static attribute.
   }

}

Do you have a solution for the final classes, too?

Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.