FAQ Database Discussion Community


Splitting big classes with Inner classes in Java

java,android,inner-classes,packaging,object-oriented-analysis
I am working on an Android project. I have searched high and low but I can't figure out a good strategy to split and package my code. There are tons of posts on how to split based on MVC, but my problem is that I have Inner Classes that use...

java - how to create custom hashtable iterator?

java,iterator,hashmap,hashtable,inner-classes
I am currently trying to implement a Hashtable collection--I have everything up and running but I ran into kind of a conceptual problem when I was trying to define a custom iterator for the table. I have an internal class called 'HashEntry' which are the actual objects stored in the...

instantiating an inner class from the main method

instantiation,inner-classes
How can I instantiate an inner class? for example: QuoteArray quotes1 = new QuoteArray(1); QuoteArray.Quote quote1 = new QuoteArray.Quote("quote","char","actor","film","year");//error here Where Quote is an inner class of QuoteArray. None of this really makes sense to me. I know how to instantiate Quote from within QuoteArray, but I don't know how...

Access deployment id of vertx verticle from outside the inner class?

java,inner-classes,vert.x
Hi i have deployed vertx verticle like this and now i want the asynchResult access this inner class. I have gone through many posts but didnt find any solution for that. container.deployVerticle("com.demo.vertx.FirstVert", snmp_trap_listener_count, new AsyncResultHandler<String>() { public void handle(AsyncResult<String> asyncResult) { if (asyncResult.succeeded()) { System.out.println("The verticle has been deployed, deployment...

Java stopped erroring on non-final variables in inner classes (java 8) [duplicate]

java,java-8,inner-classes
This question already has an answer here: Difference between final and effectively final 5 answers Java 7 was saying "Cannot refer to the non-final local variable message defined in an enclosing scope" on following code: public class Runner { public static void main(String[] args) { String message = "Hello...

jni doesn't find method in inner class , java.lang.NoSuchMethodError

java,c++,jni,inner-classes,nosuchmethoderror
I have a question for getting access to methods in nested classes with JNI. public class AccountUI { public native ExtrasWageUI[] getExtrasWages(); public class ExtrasWageUI { public ExtrasWageUI() { mCppHandle = callConstructorN(); } public ExtrasWageUI(long cppHandle) { mCppHandle = cppHandle; } private native long callConstructorN(); } } I will get...

Inner Class Instantiate Clarification

java,swing,instantiation,inner-classes
I don't understand a feature of the inner class CHameleon. I don't get what line 8 in theory means. I think it means to produce a duplicate version of JFrame that will be accessed outside of the inner class, in order to manipulate the intended JFrame object. Edit: Code brings...

How to avoid ignore tests if I use inner classes for unit testing architecting?

java,junit,mockito,inner-classes,runner
I have following architecture of unit test: @RunWith(Enclosed.class) public class ProductTest { @RunWith(MockitoJUnitRunner.class) public static abstract class Base {...} public static class Test1 extends Base{ @Test public void foo(){...} } } public static class Test2 extends Base{ @Test public void bar(){...} } } } If I run unit tests I...

Variable is accessed from within inner class , needs to be declared final

android,checkbox,inner-classes,final
I am using multiple check boxes and need to perform different operations on checked i.e increment some static variables according to the checkbox selected. So the problem is i cant make the variable i to be final (using a loop) , otherwise it is not accessible in the inner class....

Can't reference inner class from a static context, but only if outer class is generic

java,generics,inner-classes
The following won't compile: class Outer<T> { class Inner { } static class Nested { Inner inner; // Error: Outer.this cannot be referenced from a static context } } However, if I remove <T>, it compiles. Why the inconsistency? Also, if I say Outer.Inner inner;, instead of Inner inner;, it...

how to access a Fragment from the inner AsyncTask

java,android,inner-classes
I have a Fragment and inner AsyncTask that loads a text: public class TextReaderFragment extends Fragment { LoadTextTask task = null; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { setRetainInstance(true); task=new LoadTextTask(); task.execute(); ... } public void renderText() { ... } class LoadTextTask extends AsyncTask<Integer, Void, Boolean> {...

I've got trouble with inner and static inner class at java

java,inner-classes
Firstly, Thanks everybody that read that topic. How can if statement become true in test class? I couldnt find any solution.I couldnt write any code in these method.I tried to send from Room class numberOfTiger to class Question's method but I didnt achieve that. That's question about ,How can I...

What is the recommended/correct way to access fields in an inner class?

java,inner-classes
Suppose we have this class and its inner class: /* Outer.java */ public class Outer { private static class Inner { private final Object foo; public Inner(Object foo) { this.foo = foo; } public Object getFoo() { return foo; } } Inner inner = parse(/* someMistery */); // Question: to...

Can I extend Iterator to allow access to encapsulated inner class fields?

java,iterator,abstract-class,inner-classes,extends
I'm frustrated, trying to add methods to a custom iterator. For an assignment in my second quarter Java class, we are supposed to implement, from scratch, a linked list. No problem: I have created an outer class to implement the linked list and a static private inner class to implement...

how to run the main thread after all child threads have completed there exceution

java,multithreading,inner-classes,anonymous-class
I have a requirement in which 28 threads have to complete some functionality. I have created these threads as in anonymous inner classes like : Thread t=new Thread(new Runnable(){public void run() {//code }} ); t.start(); Now I want that the further execution should start after all these threads have finished...

Inner class property make read only to all accessing classes except outer class

c#,inner-classes,readonly
I am new to c# and trying to do some coding when I stumbled on this. I don't know how to word it.So first the code.(It is dummy code just to explain my question). public class DatabaseConnector { public Caching Cache {get;set;} //More properties //some methods public DatabaseConnector(string[] paramters) {...

Nested abstract class in an abstract class and how to implement it

c#,abstract-class,inner-classes,abstraction
I have an abstract class A and a abstract method with a parameter which is again abstract class B defined in the same abstract class A. When I extended this abstract class A as apart of another class C how can I implement the method with parameter of nested abstract...

How do I add different inner classes to an ArrayList?

java,arraylist,inner-classes
So, I'm building a very basic program based on the Warhammer 40k tabletop game as an extra credit assignment for my Comp Sci class. I get to develop the specs and then I implement the specs. In this game, I have a class Army that has an ArrayList of class...

Is outer derived class visible through C#.NET reflection of inner class inherited from parent?

c#,.net,inheritance,reflection,inner-classes
In the following code example I want to inherit class B from class A and be able to get name of class B if I have only object of type B.AC. So my subject question can be specified as: Is class B visible through C#.NET reflection of B.AC ? Based...

Inner class declaration and initialization

java,class,object,nested,inner-classes
I am used to code in C++, but have to convert a project from C++ to Java. In C++ using data structure is pretty much simple. I am trying to replicate the same thing, but such as a Java inner class and static nested class. After reading several examples online,...

java best practice -outer class static variable accessed by inner class

java,garbage-collection,inner-classes
class OuterClass { static int j=99; static class InnerClass { public void doSomething() { while(true) OuterClass.j= 100; } } } } Using the above code, is there any issue with garbage collection when the OuterClass needs to garbage collect but lets say doSomething is referencing a static variable will it...

Passing to Setter and Getter methods

java,methods,inner-classes,setter,getter
I have a Bread class and a Filling class which set the bread type and calories per slice as well as a filling class which sets the filling type and calories per serving... I can't seem to figure out how to pass them into the sandwich class correctly. My total...

Accessibility of inner class members from enclosing class

java,iterator,inner-classes,access-modifiers
I thought members of an inner class, even when declared private, is accessible from its enclosing class. But I am running into compile time error with the following code structure. My top level class implements Iterable interface. The Iterator is implemented in an inner class. When an instance of the...

Setting outer class instance to null, and yet inner class instances is able to access outer class's field. How?

java,nested,closures,inner-classes
Consider the following piece of code: class OuterClass{ String ocs="ocs"; class InnerClass{ String ics="ics"; void innerMeth(){ System.out.println(ocs); } } } //main class public class HelloWorld{ public static void main(String []args){ OuterClass ob=new OuterClass(); OuterClass.InnerClass ob1=ob.new InnerClass(); ob=null; ob1.innerMeth(); } } Still prints out string 'ocs' . basically even though I...

abstract private inner class

java,abstract-class,private,inner-classes
I am preparing for an Oracle examination and answered incorrectly to the following question: the combination abstract private is legal for inner classes As it turns the answer is true, I answered false, as I could not find any use cases for having an abstract private inner class, that cannot...

How to access outer class variable of same name?

java,encapsulation,inner-classes,nested-class
I have made an Outer and an Inner class. Both these classes have variable int x. How to access x of Outer class in inner class. this.x is not working. class OuterClass { int x,y; private class InnerClass { private void printSum(int x,int y) { this.x=x; this.y=y; } } }...

C++: static instance of inner class in outer class

c++,static,inner-classes
I have written some piece of code as below. Here there is an outer class and an inner class. I want a functionality similar to inner static class of java. So I declare the inner class object as static. The output comes fine: #include <iostream> using namespace std; class X...

Adding objects to an ArrayList from another class [duplicate]

java,arraylist,nullpointerexception,inner-classes
This question already has an answer here: What is a Null Pointer Exception, and how do I fix it? 12 answers I am trying to add objects to an ArrayList from another file but I am getting a java.lang.NullPointerException even though I have initialized the ArrayList. Here is an...

How inner class object resides in memory?

java,inner-classes
Outer outer = new Outer(); an Object of Outer class is created on heap and reference variable points to it. If I understand it right when I write Outer.Inner inner=outer.new Inner(); an object of Inner class is created on heap and inner points to it. In heap we have two...

C++: How can I access a method of an inner class?

c++,function,class,inner-classes
I was going through classes and thought of doing nested classes.So I have the below class declaration. class myClass { public: void method1() { cout << "This is method 1" << endl; } class myClass2 { void method2() { cout << "This is method 2" << endl; } }; };...

Referencing non-final variable: why does this code compile?

java,java-8,inner-classes
First off, I apologise if this is a duplicate question. I found many similar ones, but none that directly address my question. In preparation for an upcoming exam, I am doing a past paper. It has a question that gives a code snippet. We have to state if it compiles,...

Inner Class has an implicit reference to the outer class and may can leak memory

java,memory-leaks,static,inner-classes
After learning about the inner class, I understand it has an implicit reference to the outer class. But my teacher told me that the best way is not use inner class, prefer to use static inner class. Because the inner class may leak memory. Can someone kindly explain about this?...

Java Generics incompatible types linkedlist iterators

java,generics,inner-classes
Trying to create an iterator for a generic linked list. When I attempt to create a 'current' node for the purpose of iterating through the list based upon the head sentinel, I get an incompatible type error. Here's the relveant lines public class LinkedList<T> implements Iterable<T> { private node<T> headsent;...