Why compiler gives me error when I call object MyObject ?

public class ThisIsClass 
{
    public static void main(String[] args)
    {
        MyObject loan = new MyObject();  //error should be ThisIsClass loan = new ThisIsClass(); 
        System.out.println(loan.toString());
    }
}

It works fine when I change name of my class to MyObject or change object name?
Why I can't use object on my own name ?

Thanks for quick explanation

Recommended Answers

All 2 Replies

That's strange. The setup works for me...

Where's your MyObject class?

Member Avatar for 1stDAN

Well, by applying "... loan = new ..." you want to create an instance (object) of a class. The only class in your file ThisIsClass.java seems to be ThisIsClass. So you can only apply ThisIsClass loan = new ThisIsClass();.

If you want to use type MyObject as a class name, you must have a class like class MyObject { ... } stored in your file ThisIsClass.java or elsewhere yet reachable by the compiler.

I think there is something of a misconception, for your chosen name MyObject suggest that you want to have an instance (= object) of a class. For object is just another name for instance (synonym) maybe this is more situable: ThisIsClass myObject = new ThisIsClass();.

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.