2

I am working with StringBuffer. Using NetBeans 8.1 IDE.

Code I wrote:

package stringbuffer;

import java.lang.*;

public class StringBuffer {
    public static void main(String[] args) {
       StringBuffer sb=new StringBuffer("hello");
    }
}

But this is showing error like this:

enter image description here

What's my problem?

1
  • 1
    That's because StringBuffer is an actual Java class, and your IDE is confused. Change the name of your class to something else. Commented Mar 9, 2016 at 4:13

1 Answer 1

3

You've named your class StringBuffer. So it shadows java.lang.StringBuffer (and you never have to import java.lang.*). Rename your class, or use the fully qualified StringBuffer name.

public class StringBuffer {
    public static void main(String[] args) {
       java.lang.StringBuffer sb=new java.lang.StringBuffer("hello");
    }
}

or

public class MyStringBuffer {
    public static void main(String[] args) {
       StringBuffer sb=new StringBuffer("hello");
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.