-1

How can I code a struct-like class that contains public variables, and then be able to access them from another class without using getters and setters?

I know how to do getter/setter but they just take a lot of typing space, so I was wondering if maybe using an inner class would eliminate that? From what I understand, static means I can have only one x variable, which I can't do, because I need an array.

public class publicClass {
    innerclass array_inner[];

    class innerclass {
        private int x;
        innerclass(int x){this.x = x;}
        ...
    }

    public publicClass {array_inner = new innerclass[5];}

    public access_x {
       array_inner[0].x; 
    }

}
ravsmy
  • 37
  • 6
  • 2
    `get` and `set` might be a lot of typing, but they are idiomatic Java. You would probably do best to conform to the idioms; you'll run into all kinds of silly edge cases if you work around them. – alephtwo Feb 21 '18 at 20:48
  • Just use public variables. So long as you understand the downsides of that. There's nothing intrinsically wrong with using writable fields. Writable properties are better and safer in several ways, but if the size of the boilerplate is important, then dump the properties and go with fields. Or go with Kotlin. – Paul Hicks Feb 21 '18 at 20:48
  • What happens when you compile this code? What errors do you get? – Code-Apprentice Feb 21 '18 at 20:48
  • So there is no way to use nested classes to get rid of the problem? – ravsmy Feb 21 '18 at 20:49
  • What is the problem? – Code-Apprentice Feb 21 '18 at 20:50
  • When I compile it, it cannot access the x from the inner class... – ravsmy Feb 21 '18 at 20:50
  • @RavelaSmyth that's simply because you've written an invalid statement in an invalid method. It has nothing to do with `x` or the inner class. – Andy Turner Feb 21 '18 at 20:54
  • which statement was invalid? sorry, this method was incomplete, i was just giving a rough example of my code – ravsmy Feb 21 '18 at 20:58

3 Answers3

0

Your code contains a bunch of mistakes and they have nothing to do with accessing inner class variables. Also, inner and nested are different notions, and as already mentioned in the answers you have wrong understanding of what is static in java. Anyway here is corrected code with no compilation errors:

public class PublicClass {
  Innerclass array_inner[];

  class Innerclass {
    private int x;

    Innerclass(int x) {
      this.x = x;
    }
  }

  public PublicClass() {
    array_inner = new Innerclass[5];
  }

  public void access_x() {
    System.out.println(array_inner[0].x);
  }

  public static void main(String[] args) {
    PublicClass publicClass = new PublicClass();
    publicClass.array_inner[0] = publicClass.new Innerclass(0);
    publicClass.access_x();
  }
}

Output:

0
Sergei Sirik
  • 1,249
  • 1
  • 13
  • 28
  • Thank you for your answer! So though it is private, i can access the x within PublicClass right? – ravsmy Feb 21 '18 at 21:11
  • Yes you can access private variables of inner class in the outer class. Consider this: https://stackoverflow.com/questions/1801718/why-can-outer-java-classes-access-inner-class-private-members. P.S. I've added some test code. – Sergei Sirik Feb 21 '18 at 21:16
0

you must make this variable static if you want to have access

0

Here the InnClass can access the OutClass methods and attributes by its own methods as implimented. So glide through the provided code, it may help you : --

class OutClass {    //Outer Class

    String a = "AutoSpy", b = "Beef", c = "Crunch";

    class InnClass{ //Inner Class
        int i;
        public String accessA(){
            return a;   // Accessing Outer class variable (a) inside inner class
        }
    }

    public static class StaticInnClass{
        int i;
    }

    public String combineString(){
        return a + b + c + " : Combined";    
    }
}

public class MainClass{

    public static void main(String args[]) {

        OutClass.StaticInnClass staticClass = new OutClass.StaticInnClass();
        OutClass outer = new OutClass();
        OutClass.InnClass inner = outer.new InnClass();
        System.out.println(inner.accessA());
    }
}
MrBhuyan
  • 151
  • 2
  • 17