inner-classes

inner class access to outer class method, same method names

▼魔方 西西 提交于 2019-12-01 03:42:44
i got a class and a subclass 01 public class A{ 02 void test(){}; 03 public class B{ 04 void test(){ 05 test(); 06 } 07 } 08 } Ok, in line 05 id like to access the method test of class A. But i go into a loop because i dont know how to specify to use the method of class A. Any ideas? 01 public class A{ 02 void test(){}; 03 public class B{ 04 void test(){ 05 test(); // local B.test() method, so recursion, use A.this.test(); 06 } 07 } 08 } EDIT : As @Thilo mentioned : Avoid using same method names in outer class and inner class, this will avoid naming conflicts. You can do something like that :

Java - Inner class constructor - allowed for outer class only

你。 提交于 2019-12-01 03:24:14
I have inner class in my code. I want to give public access to its instances, but only outer class should be able to create this instances, like in "private" access. Is it possible without making properly small package (or creating public interface for every such inner class)? (Sorry if my english is bad :P) It is possible. Declare your inner class public, but its constructor private . This way you can create it only inside your enclosing class and itself, but not from outside. By default,If you want to get the instance of the inner class you need to have the Outer class first. A inner class

Import inner enum in Groovy script

 ̄綄美尐妖づ 提交于 2019-12-01 03:08:57
问题 I have a Groovy class defined in Vehicles.groovy that contains some inner enums: public class Vehicles { public enum Land { BICYCLE, CAR, TRAIN } public enum Water { SAILBOAT, MOTORBOAT } public enum Air { JET, HELICOPTER } } I'd like to reference these enums in a script run.groovy in the same directory as Vehicles.groovy . Fully qualifying the enum instance works. import Vehicles println Vehicles.Land.BICYCLE or import static Vehicles.Land println Vehicles.Land.BICYCLE or import Vehicles

Why should I use nested classes? [closed]

天涯浪子 提交于 2019-12-01 03:05:56
When is it feasible to nest classes? The most common advantage of it that I see is "shared scope" (use of variables across classes). Is this less attractive/less a best practice than just putting the nested class in it's own file, and passing the arguments through the Constructor? dd619 There are several reasons for using nested classes , among them: It is a way of logically grouping classes that are only used in one place. It increases encapsulation . Nested classes can lead to more readable and maintainable code . Child to parent class connection is simpler as it visually illustrates the

Creating instance of inner class outside the outer class in java [duplicate]

孤街浪徒 提交于 2019-12-01 02:29:01
问题 This question already has answers here : Java - No enclosing instance of type Foo is accessible (5 answers) Closed 5 years ago . I'm new to Java. My file A.java looks like this: public class A { public class B { int k; public B(int a) { k=a; } } B sth; public A(B b) { sth = b; } } In another java file I'm trying to create the A object calling anotherMethod(new A(new A.B(5))); but for some reason I get error: No enclosing instance of type A is accessible. Must qualify the allocation with an

Constructors in Inner classes (implementing Interfaces)

佐手、 提交于 2019-12-01 02:13:39
How would I go about writing a constructor for an inner class which is implementing an interface? I know I could make a whole new class, but I figure there's got to be a way to do something along the line of this: JButton b = new JButton(new AbstractAction() { public AbstractAction() { super("This is a button"); } public void actionPerformed(ActionEvent e) { System.out.println("button clicked"); } }); When I enter this it doesn't recognize the AbstractAction method as a constructor (compiler asks for return type). Does anyone have an idea? Just insert the parameters after the name of the

inner class access to outer class method, same method names

柔情痞子 提交于 2019-12-01 00:47:53
问题 i got a class and a subclass 01 public class A{ 02 void test(){}; 03 public class B{ 04 void test(){ 05 test(); 06 } 07 } 08 } Ok, in line 05 id like to access the method test of class A. But i go into a loop because i dont know how to specify to use the method of class A. Any ideas? 回答1: 01 public class A{ 02 void test(){}; 03 public class B{ 04 void test(){ 05 test(); // local B.test() method, so recursion, use A.this.test(); 06 } 07 } 08 } EDIT : As @Thilo mentioned : Avoid using same

Java: Should serializable inner & anonymous classes have SerialVersionUID?

末鹿安然 提交于 2019-11-30 22:25:01
Although I'm not currently planning to serialize anything, I give all serializable outer classes, as well as static nested classes a SerialVersionUID , because that is the proper way to do it. However, I've read here that Serialization of inner classes (i.e., nested classes that are not static member classes), including local and anonymous classes, is strongly discouraged for several reasons. ... So my question is: Should I give inner and anonymous classes a SerialVersionUID each, or should I add a @SuppressWarnings("serial") to those? Is one way more proper than the other? I will in any case

Constructors in Inner classes (implementing Interfaces)

我的梦境 提交于 2019-11-30 21:45:10
问题 How would I go about writing a constructor for an inner class which is implementing an interface? I know I could make a whole new class, but I figure there's got to be a way to do something along the line of this: JButton b = new JButton(new AbstractAction() { public AbstractAction() { super("This is a button"); } public void actionPerformed(ActionEvent e) { System.out.println("button clicked"); } }); When I enter this it doesn't recognize the AbstractAction method as a constructor (compiler

Static inner classes need import for annotations

半城伤御伤魂 提交于 2019-11-30 21:20:11
So I was doing some jUnit testing and wanted to write distinct classes that had similar functionality but were small enough to write within a single class. Regardless of the decision for design it brought me to a compiler error I am not sure what the rules are for what I saw. You can imagine it would look something like package foo; @RunWith(Suite.class) @SuiteClasses({ TestClassOne.class, TestClassTwo.class }) public class TestSuite{ @RunWith(SpringJUnit4ClassRunner.class) public static class TestClassOne{ } @RunWith(SpringJUnit4ClassRunner.class) public static class TestClassTwo{ } } Now