简体   繁体   中英

Why is this error in parameterized constructor?

alright , heres the code :

public class MyClass {

long var;
public void MyClass(long param)
{
var=param; //st1
}

public static void main(string args[])
{
MyClass a,b;
a=new MyClass(); //st2
b=new MyClass(5); //st3
}

}

why error occurs at st3 instead of line st2 ?

This:

public void MyClass(long param)

declares a method called MyClass , whereas I suspect you wanted a constructor. Remove the return type:

public MyClass(long param)

At that point I suspect you'll get the behaviour you expect.

Personally I think it's a design flaw that Java allows you to declare a method with the same name as the containing type, but that's a different matter.

Also note that

main(string args[])

should be

main(String args[])

or more conventionally:

main(String[] args)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM