开发者

Add class in java

开发者 https://www.devze.com 2022-12-15 03:53 出处:网络
I use NetBeans 6.7.1 and I\'m more used to C and php than Java. I have a .class file containing \"public class myclass\". And in that class I have a priv开发者_开发知识库ate array and a public functi

I use NetBeans 6.7.1 and I'm more used to C and php than Java.

I have a .class file containing "public class myclass". And in that class I have a priv开发者_开发知识库ate array and a public function getArr returning that array.

In the main class I have this

Object mcl = new myclass();

myclass is found by NetBeans and the above is accepted. But

String[] arr = mcl.getArr();

throws "Cannot find symbol" for getArr.

The file containing that class is in the project and the class found in code completion. But not the function, or I should probably call it a method.

I don't know if this is Java or NetBeans specific. But how do I get the main class to understand what it can find in my new class? And the compiler?

Am I missing some declaration?


You're defining mcl as an Object, which doesn't have a getArr() method -- myclass does. You need to do:

myclass mcl = new myclass();

Then you will be able to refer to all the methods of myclass.

The reason that you're able to define it as an Object is because all classes in Java automatically extend Object, so it is their superclass. But when you define an object as Object instead of their actual class, you only get to use the methods provided by superclass Object.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号