inner-classes

Scope of final local variable in java

戏子无情 提交于 2019-12-01 17:20:56
Method-Local inner class cannot access local variables because the instance of the method-local inner class may still alive after the method is over. But local variables will vanish once the local method is over. I learned that method-local inner class can access final local variable, does this mean final local variable still alive after the method is over? Alexis King Sort of. Java anonymous inner classes act like "closures," that is, they "close" around the current local state. However, Java only lets these classes close around final variables. If it didn't, the local state of the variable

Nested inner Activity class in android

人走茶凉 提交于 2019-12-01 16:25:40
Is declaring a class that extends Activity inside another Activity class possible? If it is, how would I register that class in the manifest? Also, is that something that can be reasonably done or is it a bad idea? I was thinking of something like class ListClass extends ListActivity{ ... ArrayList items; class ItemClass extends Activity{ ... Item item; @Override onCreate(){ Integer pos = getIntent().getExtras().getInt("pos"); item = items.get(pos); } } @Override onItemClick(int position){ startActivity(new Intent(this, ItemClass.class).putExtra("pos", position)); } } Note the syntax isn't 100

Does an inner class work as a composition relationship in java? [closed]

房东的猫 提交于 2019-12-01 15:31:53
The inner class is always inside the outer class. If we removed the outer class, the inner class would also be destroyed. I'm not concerned about memory release, I am only thinking about the concept. And this is the same concept as composition. IIn this case, the inner class must be the example of a composition relationship of objects in the real world. In short, how can I relate inner classes to object orientation in real life? By this, I mean a non-technical example; perhaps a metaphor related to animals or video games? If constructors are a good example of composition, then what is the

What is a static nested class? [duplicate]

♀尐吖头ヾ 提交于 2019-12-01 13:50:27
Possible Duplicate: Java: Static vs non static inner class What is a static nested class? What is the difference between static and non-static nested classes? A static inner class is a class nested inside another class that has the static modifier. It's pretty much identical to a top-level class, except it has access to the private members of the class it's defined inside of. class Outer { private static int x; static class Inner1 { } class Inner2 { } } Class Inner1 is a static inner class. Class Inner2 is an inner class that's not static. The difference between the two is that instances of

will java outer class compilation effects inner class

删除回忆录丶 提交于 2019-12-01 06:55:08
I need to give a patch to websphere. where I have a main java class which has three inner classes. I have few code changes to main class but no changes to any inner classes. Now my question is should I need to give all innerclasses along with main class file as a part of patch or main class alone sufficient? This is not an authoritative answer, but every time I did such patching, I copied all the classes (outer and inner classes), i.e. Outer.class Outer$1.class // These indexes might change ... Outer$2.class // ... between compilation runs Outer$Inner.class // This name should never change

Import inner enum in Groovy script

橙三吉。 提交于 2019-12-01 06:27:49
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.Land.* println Vehicles.Land.BICYCLE correctly print BICYCLE . However, I'd like to reference the Land

will java outer class compilation effects inner class

一曲冷凌霜 提交于 2019-12-01 04:44:00
问题 I need to give a patch to websphere. where I have a main java class which has three inner classes. I have few code changes to main class but no changes to any inner classes. Now my question is should I need to give all innerclasses along with main class file as a part of patch or main class alone sufficient? 回答1: This is not an authoritative answer, but every time I did such patching, I copied all the classes (outer and inner classes), i.e. Outer.class Outer$1.class // These indexes might

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

二次信任 提交于 2019-12-01 04:37:11
This question already has an answer here: Java - No enclosing instance of type Foo is accessible 5 answers 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 enclosing instance of type A (e.g. x.new B() where x is an instance of A). Can someone explain how can I do what I want to do?

“Default member initializer needed within definition of enclosing class outside of member functions” - is my code ill-formed?

╄→гoц情女王★ 提交于 2019-12-01 04:00:50
struct foo { struct bar { ~bar() {} // no error w/o this line }; bar *data = nullptr; // no error w/o this line foo() noexcept = default; // no error w/o this line }; Yes, I know, there is another question with exactly the same title, but a somewhat different problem (involving a noexcept operator and no nested type). The solution suggested there (replacing the constructor of foo with foo() noexcept {} ) changes the semantics and it not necessary here: here we have a better answer (hence the question is not a duplicate). compiler: Apple LLVM version 9.0.0 (clang-900.0.37) , full error message:

Why can't an inner class use static initializer?

爷,独闯天下 提交于 2019-12-01 03:43:59
问题 Quoth JLS #8.1.3: Inner classes may not declare static initializers (§8.7)...... This is demonstrated as such: class A { class B { static { // Compile-time Error: Cannot define static initializer in inner type A.B System.out.println("Class is initializing..."); } } } Now since Java's inner (non-static) classes are loaded by class loaders just like every other class, why can't we have static initializers for them? What's the reason behind this limitation? 回答1: I think it is because the Inner