1

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.

1
  • there is a way to do what you want look at my answer. You can try casting to each class one by one catching ClassCastException if it is thrown and trying to cast to the next class if it is thrown. You can do this until you find the right class. If you don't want to create a parent interface or class then I would suggest trying this... Commented Aug 25, 2015 at 14:41

4 Answers 4

1

Object is a very generic class, and it doesn't have any field called a. You have some solutions to this:

  • Make the parameter be a NewFile instead of any object. This is the better one in my opinion because, apparently, puaa is made to operate only on a NewFileobject.

    public String puaa(NewFile o) {
    
  • Cast o to NewFile. I don't recommend this, because if you pass something that's not a NewFile instance it will throw an exception, and so it's better the first solution than this.

    System.out.println(">> " + ((NewFile) o).a);
    
Sign up to request clarification or add additional context in comments.

7 Comments

What if you don't send a NewFile object reference, but reference to other object which is stucturly same like OldFile() . I want reference which could hold any type and which convert into any type
@fatherazrael that cannot be achieved. Java has strong typing. Unless OldFile extends Newfile, this won't happen, but you have to understand that that will be a very odd design.
Then, make OldFile inherit NewFile or make both inherit a new class, which will then be the parameter type.
i am trying using another interface which implements by Newfile and Oldfile. But still getting an issue i.e that interface should have variable a declared in it
You must declare a in that interface you are creating. Also note the parameter type will have to be that interface now.
|
1

You cannot do that. Object can be anything: a String, an Integer, a HashMap, an ExecutorService... and from these 4 examples, none of them are closely related to your NewFile class.

The best alternative will be declaring a super (abstract maybe) class or an interface for your classes and pass an argument of this super type to the method.

Here's a small example:

public abstract class MyFile {
    protected String a;

    public MyFile(String a) {
        this.a = a;
    }

    public String getA() {
        return a;
    }
}

public class NewFile extends MyFile {
    public MyFile(String a) {
        super(a);
    }
}

public class OldFile extends MyFile {
    /* add your implementation... */
}

public class Main {
    public static void main(String[] args) {
        //you can replace here by using new OldFile
        MyFile myFile = new NewFile("foo");
        Main main = new Main();
        main.puaa(myFile);
    }

    public void puaa(MyFile myFile) {
        System.out.println(myFile.getA());
    }
}

Comments

1

create interface

public interface HasA { 
  String getA();
}

Add this interface on all classes that you want to use with puaa

public void puaa(HasA o){
  System.out.println(">> " + o.getA());
}

Comments

-1

You need to cast it to a NewFile object. It is looking at the base Java class Object for a method a which doesn't exist.

System.out.println(">> " + ((NewFile)o).a);

You can try casting to each class one by one catching ClassCastException until you get the class you want.

5 Comments

@LuiggiMendoza then you handle the ClassCastException that is thrown.
It won't compile since Object do not have attribute "a"
@sunrise76 it compiles due to the downcasting.
@LuiggiMendoza like I said before you handle the thrown Exception. It depends on what OP is trying to do.
@Emd4600 never said it wasn't better. Just gave him a quick easy answer. And if you read his other comments this is actually what he wants.

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.