Inner class declaration and initialization - java

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, and trying different versions, so far this is what I got:
public class Main {
public static void main( String[] args ) {
...
ClassOuter outerObj = new ClassOuter();
ClassOuter.DataInner value = outerObj.new ClassOuter.DataInner();
}
}
class ClassOuter{
public static class DataInner{
public int x;
}
...
protected void getNo()
{ value.x=Integer.parseInt("493");
}
}
However, when I try to compile, it gives me the error:
$ javac -cp "./" Main.java
Main.java:15: error: '(' expected
ClassOuter.DataInner value = outerObj.new ClassOuter.DataInner();
Any clue about what is missing here?

ClassOuter.DataInner value = outerObj.new ClassOuter.DataInner();
This syntax applies to inner classes (i.e. non static nested classes). If that's what you want, remove the static keyword from public static class DataInner.
EDIT :
Also change
ClassOuter.DataInner value = outerObj.new ClassOuter.DataInner();
to
ClassOuter.DataInner value = outerObj.new DataInner();
You don't specify the outer type when using an enclosing instance to initialize the inner instance.
And the line outerObj.value.x=Integer.parseInt("493"); is not valid inside your outer class's getNo() method, since outerObj and value are local variables known only to your main method.
If you wish your outer instance to update any of its inner instances, it must obtain a reference to it. Here's one way to do it :
public class Main {
public static void main( String[] args ) {
...
ClassOuter outerObj = new ClassOuter();
ClassOuter.DataInner value = outerObj.new DataInner();
outerObj.setInner (value);
}
}
class ClassOuter{
public static class DataInner{
public int x;
}
...
private DataInner inner = null;
public void setInner (DataInner inner) {
this.inner = inner;
}
protected void getNo()
{
inner.x=Integer.parseInt("493");
}
}

If DataInner must be static class:
public class Main {
public static void main(String[] args) {
ClassOuter outerObj = new ClassOuter();
ClassOuter.DataInner value = new ClassOuter.DataInner();
}
}
class ClassOuter {
public static class DataInner {
public int value;
}
}
It this case, DataInner has no reference to the ClassOuter instance.
If DataInner must not be static class.
public class Main {
public static void main(String[] args) {
ClassOuter outerObj = new ClassOuter();
ClassOuter.DataInner value = outerObj.newInner();
}
}
class ClassOuter {
public class DataInner {
public int value;
}
public DataInner newInner() {
return new DataInner();
}
In this case, DataInner has reference to the ClassOuter instance (ClassOuter.this).

Related

Can outclass specify inner class with InnerClassNameOuter directly rather than ClassName.InnerClassName

In Inner classes of Thinking In Java,
If you want to make an object of the inner class anywhere except from
within a non-static method of the outer class, you must specify the
type of that object as OuterClassName.InnerClassName, as seen in
main().
But I find use InnerClassName directly still works in main.
public class Parcel2 {
class Contents {
private int i = 42;
public int value() { return i; }
}
class Destination {
private String label;
Destination(String whereTo){
label = whereTo;
}
String readLabel(){ return label; }
}
public Destination to(String s){
return new Destination(s);
}
public static void main(String[] args){
Parcel2 q = new Parcel2();
/* Destionation d = q.to("Borneo"); still works.*/
Parcel2.Destination d = q.to("Borneo");
}
}

Passing parameter to anonymous class in Java

i'm trying to write anonymous inner class
interface Face{
void seeThis(String what);
}
class Eyes {
public void show(Face f){}
}
public class Seen {
public void test() {
Eyes e = new Eyes();
e.show(new Face() {
#Override
public void seeThis(String what){
System.out.print(what);
}
});
public static void main(String[] args) {
Seen s = new Seen();
s.test();
}
}
How to call seeThis() and how to pass parameter to it?
Method seeThis() belongs to Face class, which instance is anonymous and thus cannot be reached without storing reference to it. If you want to store a reference, you can do this in the following way:
public class Seen {
public Face face;
....
this.face = new Face() { ... };
e.show(this.face);
And then,
Seen s = new Seen();
s.face.seeThis();
Now, regarding passing the parameter. You have two options - declare parameter outside of anonymous class and make it final in order to be reachable by this anonymous class, or replace anonymous class with normal one and pass the parameter to its constructor:
Approach one:
final int parameter = 5;
...(new Face() {
#Override
public void seeThis() {
System.out.println(parameter);
}
});
Approach two:
public class MyFace implements Face() {
private final int parameter;
public MyFace(int parameter) {
this.parameter = parameter;
}
#Override
public void seeThis() {
System.out.println(parameter);
}
}
Then,
...
e.show(new MyFace(10));

Accessing variable from an inner class - getting null pointer exception

i want to access the arr variable from inside the inner class method MyMethod. When i try to print it from there i end up getting a null pointer exception.
public class MyClass{
String[] arr;
MyClass my;
public MyClass(){
my = new MyClass();
}
public class MyInner {
public void MyMethod() {
// I need to access 'my.arr' from here how can i do it.
}
}
public static void main(String[] args) {
String[] n={"ddd","f"};
my.arr=n;
}
}
You can use just arr. However until you set it to something it will be null
BTW: Your my = new MyClass() will blow up as it will create objects until it stack overflows.
You haven't initialized it yet, so the reference is null. Initialize it in your constructor for example, and you will have access to the variable via your inner class.
public class MyClass {
String[] arr;
public MyClass (String[] a_arr) {
arr = a_arr;
}
public class MyInner {
public void MyMethod () {
// I need to access 'my.arr' from here how can i do it.
}
}
public static void main (String[] args) {
String[] n= {"ddd","f"};
MyClass myClass = new MyClass (n);
}
}
Well, for starters in your main method you never create an instance of your class.
Also, MyClass has a reference to a MyClass object. In the constructor of MyClass, it initializes that reference by calling it's own constructor. That's an endless loop.
Do the following. Your way of initialization is wrong.
public class MyClass{
String[] arr;
MyClass my;
public MyClass(){
}
public class MyInner {
public void MyMethod() {
// I need to access 'my.arr' from here how can i do it.
}
}
public static void main(String[] args) {
String[] n={"ddd","f"};
MyClass my=new MyClass();
String[] b = new String[2];
System.arraycopy( n, 0, b, 0, n.length );
}
}
In case of more than 2 strings, simply do String[] b = new String[n.length];

Cannot compile inner class source files, resulting in error?

The seem not to be able to compile the following main method:
public class MainMethod {
public static void main(String []args){
InnerizationClass outer = new InnerizationClass();
InnerizationClass.StaticInnerClass inner = outer.StaticInnerClass(); //#1
System.out.println(inner.getOuterClassVar());
System.out.println(new InnerizationClass().locInnrMeth());
}
}
And this is the InnerizationClass class:
public class InnerizationClass {
String outerClassVar = "Outer Catism";
static String outerClassVarStat = "Outer Static Catism";
public static class StaticInnerClass{
String innerClassVar = "Catism";
public String getInnerClassVar(){
return this.innerClassVar;
}
public String getOuterClassVar(){
return InnerizationClass.outerClassVarStat;
}
}
public String locInnrMeth(){
class MethodLocalInnerClass{
String methodLocalInrCls = "Method Local Catism";
void printInner(){
System.out.println(InnerizationClass.this.outerClassVar);
}
}
return new MethodLocalInnerClass().methodLocalInrCls;
}
public String getStaticOuterVar(){return null;}
}
I am getting the following error from #1 from the main method:
MainMethod.java:6 qualified new of static class
InnerizationClass.StaticInnerClass inner = outer.new StaticInnerClass();
1 error;
Try this instead:
public class MainMethod {
public static void main(String []args){
InnerizationClass.StaticInnerClass inner = new InnerizationClass.StaticInnerClass(); //#1
System.out.println(inner.getOuterClassVar());
System.out.println(new InnerizationClass().locInnrMeth());
}
}
since your inner class is static you don't need to create an instance of InnerizationClass

Java: Anonymous inner class using a local variable

How can I get the value of userId passed to this method in my anonymous inner subclass here?
public void doStuff(String userID) {
doOtherStuff(userID, new SuccessDelegate() {
#Override
public void onSuccess() {
Log.e(TAG, "Called delegate!!!! "+ userID);
}
});
}
I get this error:
Cannot refer to a non-final variable userID inside an inner class defined in a different method
I'm pretty sure I can't assign it as final since it's a variable with an unknown value. I had heard that this syntax does preserve scope in some way, so I think there must be a syntax trick I don't quite know yet.
As everyone else here has said, local variables have to be final to be accessed by an inner class.
Here is (basically) why that is... if you write the following code (long answer, but, at the bottom, you can get the short version :-):
class Main
{
private static interface Foo
{
void bar();
}
public static void main(String[] args)
{
final int x;
Foo foo;
x = 42;
foo = new Foo()
{
public void bar()
{
System.out.println(x);
}
};
foo.bar();
}
}
the compiler translates it roughly like this:
class Main
{
private static interface Foo
{
void bar();
}
public static void main(String[] args)
{
final int x;
Foo foo;
x = 42;
class $1
implements Foo
{
public void bar()
{
System.out.println(x);
}
}
foo = new $1();
foo.bar();
}
}
and then this:
class Main
{
private static interface Foo
{
void bar();
}
public static void main(String[] args)
{
final int x;
Foo foo;
x = 42;
foo = new $1(x);
foo.bar();
}
private static class $1
implements Foo
{
private final int x;
$1(int val)
{
x = val;
}
public void bar()
{
System.out.println(x);
}
}
}
and finally to this:
class Main
{
public static void main(String[] args)
{
final int x;
Main$Foo foo;
x = 42;
foo = new Main$1(x);
foo.bar();
}
}
interface Main$Foo
{
void bar();
}
class Main$1
implements Main$Foo
{
private final int x;
Main$1(int val)
{
x = val;
}
public void bar()
{
System.out.println(x);
}
}
The important one is where it adds the constructor to $1. Imagine if you could do this:
class Main
{
private static interface Foo
{
void bar();
}
public static void main(String[] args)
{
int x;
Foo foo;
x = 42;
foo = new Foo()
{
public void bar()
{
System.out.println(x);
}
};
x = 1;
foo.bar();
}
}
You would expect that foo.bar() would print out 1 but it would actually print out 42. By requiring local variables to be final this confusing situation cannot arise.
Sure you can assign it as final - just put that keyword in the declaration of the parameter:
public void doStuff(final String userID) {
...
I'm not sure what you meant about it being a variable with an unknown value; all that final means is that once a value is assigned to the variable, it cannot be re-assigned. Since you're not changing the value of the userID within your method, there's no problem making it final in this case.
In Java 8, this has changed a little bit. You can now access variables that are effectively final. Relevant snippet and example from the Oracle documentation (emphasis mine):
However, starting in Java SE 8, a local class can access local
variables and parameters of the enclosing block that are final or
effectively final.
Effectively final: A non-final variable or parameter whose value is never changed after it is initialized is effectively final.
For example, suppose that the variable numberLength is not declared final, and you
add the highlighted assignment statement in the PhoneNumber
constructor:
PhoneNumber(String phoneNumber) {
numberLength = 7; // From Kobit: this would be the highlighted line
String currentNumber = phoneNumber.replaceAll(
regularExpression, "");
if (currentNumber.length() == numberLength)
formattedPhoneNumber = currentNumber;
else
formattedPhoneNumber = null;
}
Because of this assignment statement, the variable numberLength is not
effectively final anymore. As a result, the Java compiler generates an
error message similar to "local variables referenced from an inner
class must be final or effectively final" where the inner class
PhoneNumber tries to access the numberLength variable:
if (currentNumber.length() == numberLength)
Starting in Java SE 8, if you declare the local class in a method, it
can access the method's parameters. For example, you can define the
following method in the PhoneNumber local class:
public void printOriginalNumbers() {
System.out.println("Original numbers are " + phoneNumber1 +
" and " + phoneNumber2);
}
The method printOriginalNumbers accesses the parameters
phoneNumber1 and phoneNumber2 of the method validatePhoneNumber
What's the problem with making it final as in
public void doStuff (final String userID)
declare the method
public void doStuff(final String userID)
The value needs to be final so that the compiler can be sure it doesn't change. This means the compiler can bind the value to the inner class at any time, without worrying about updates.
The value isn't changing in your code so this is a safe change.

Categories

Resources