1

I want to access the setters and getters in MediaBean class from class3, should I define MediaBean class in a separate file?

public class Class1
  {
     public static class MediaBean {
     private String GID;

     public String getGID() {
        return GID;
     }

     public void setGID(String GID) {
        this.GID = GID;
     }

     }
  }

class Class3{
        public static void main(String[] args) {
           Class1.MediaBean mediaBean=new Class1.MediaBean();
           mediaBean.???
        }
     }

Why I cannot access to the setters and getters?

Davoud
  • 2,576
  • 1
  • 32
  • 53

1 Answers1

0

You're invoking getters and setters in the attribute declaration section without actually declaring a new attribute.

You can either create a new attribute like this:

String myString=mediaBean.getGID();

Or invoke them inside a method:

class Class3
{
   Class1.MediaBean test=new Class1.MediaBean();
   void a()
   {
      test.getGID();
   }
}

EDIT: You've just update your code, you forgot to add the Class keyword before Class3 and Class1

Sith
  • 134
  • 9
  • Still I cannot get access to the methods inside MediaBean class. – Davoud Sep 18 '16 at 11:34
  • Error is: Cannot resolve method "getGID()". Why I cannot make class1 static? – Davoud Sep 18 '16 at 11:53
  • 1
    @Patzu There is no Class2 in your code, do you mean Class3? EDIT: See http://stackoverflow.com/questions/7370808/why-cant-a-top-level-class-be-static-in-java – Sith Sep 18 '16 at 11:57
  • @Patzu I ran your code on NetBeans and I don't get any errors (other than the missing `Class` keyword). Try rebuilding your project – Sith Sep 18 '16 at 12:05