Could any one please help in following:
HelloWorld.java
public class HelloWorld {
public static void main(String []args) {
System.out.println("Hello World");
HelloWorld he = new HelloWorld();
Newfile ne = new Newfile();
System.out.println(ne.a);
he.puaa(new Newfile());
}
public String puaa(Object o) {
System.out.println(">> " + o.a);
return null;
}
}
Newfile.java
public class Newfile {
String a="paa";
public static void main(String []args) {
System.out.println("Hello World");
}
}
I am passing NewFile() to Object class and trying to access variable of that object using Object's reference and i am getting
HelloWorld.java:12: error: cannot find symbol
System.out.println(">> " +o.a);
^
symbol: variable a
location: variable o of type Object
Update: I want to pass any object to puaa(Object o) method and that object will have variable a always. So solution should be like that it should work for all not for NewFile only.
ClassCastExceptionif it is thrown and trying to cast to the nextclassif it is thrown. You can do this until you find the right class. If you don't want to create a parent interface orclassthen I would suggest trying this...