carookee - group communication for you
Home / JavaForum / Java allgemein
Infos   |   Features   |   Gold-Edition   |   Kundenservice   
java
  Übersicht
  Forum
Beginner
Java allgemein
JDBC
JNI
Networking
Online-Ressourcen
Swing + AWT
XML
Meckerecke
  Mitglieder
LOGIN





· Passwort vergessen
· Kostenlos anmelden
  Information
  Demo
  Features
  Im Vergleich
  Anmeldung
SUCHE
Beiträge, Foren oder Verfasser finden:
  Kundenservice
  Impressum
  Datenschutz
  AGB
Status
5.081 User online
1 User eingeloggt
 

Beiträge
   Antworten     Neuer Beitrag    

Beitrag 122 von 2212 (6%) |<   <   >   >|

Autor a0hirz
Datum 31.12.05, 11:36
Betreff Anonyme Klasse, verwenden von Attributen des umliegenden Blocks


Wie können die objects der statischen Methode main(String[]) in der implementierten Methode execute der anonymen Klasse verwendet werden?

The class is:

package a0hirz.multiproject.cyclic;

import java.util.LinkedList;

public abstract class Cnt implements Cyclic
{
public static Object[] OBJECTS = Cnt.makeTestObjects();

public static void main(String[] args)
{
final Object[] objects = Cnt.makeTestObjects();

try
{
System.out.println("Test by for-cycle, referring to objects of method-block");

System.out.println(objects == null);

for (int cnt = 0; cnt < objects.length; cnt++)
{
System.out.println(cnt);
Object object = objects[cnt];
System.out.println(object);
}

}
catch (NullPointerException e)
{
e.printStackTrace();
}

try
{
System.out.println("Test by Cnt-Instance, referring to objects of method-block, that this works is my main target");

new Cnt(objects.length)
{

{
System.out.println("Dont know, why this constructor is not called");
System.out.println(objects == null);
}

public boolean execute(int[] cnt)
{
System.out.println(cnt[0]);
Object object = objects[cnt[0]]; // doesnt work
System.out.println(object);
return false;
}

};
}
catch (NullPointerException e)
{
e.printStackTrace();
}

try
{
System.out.println("Test by Cnt-Instance, referring to objects of Class Cnt");

new Cnt(OBJECTS.length)
{

{
System.out.println(OBJECTS == null);
}

public boolean execute(int[] cnt)
{
System.out.println(cnt[0]);
Object object = OBJECTS[cnt[0]];
System.out.println(object);
return false;
}

};
}
catch (NullPointerException e)
{
e.printStackTrace();
}

try
{
new Cnt(Cnt.makeTestObjects().length)
{

Object[] cntObjects;

{
this.cntObjects = Cnt.makeTestObjects();

System.out.println(this.cntObjects == null);
}

public boolean execute(int[] cnt)
{

System.out.println(cnt[0]);
Object object = this.cntObjects[cnt[0]]; // doesnt work
System.out.println(object);
return false;
}

};
}
catch (NullPointerException e)
{
e.printStackTrace();
}

}

public static Object[] makeTestObjects()
{
return new Object[] { new Object()
{
public String toString()
{
return "first";
}
}, new Object()
{
public String toString()
{
return "second";
}
}, new Object()
{
public String toString()
{
return "last";
}
} };
}

protected int[] start;

protected int[] stop;

protected int cntDepth;

public Cnt(int stop)
{
this(0, stop);
}

public Cnt(int start, int stop)
{
int[] startA = { start };
int[] stopA = { stop };

this.start = startA;
this.stop = stopA;

this.init(startA, stopA, true);
}

public Cnt(int[] start, int[] stop)
{
this.init(start, stop, true);
}

public Cnt()
{
// TODO Auto-generated constructor stub
}

private void init(int[] start, int[] stop, boolean doCnt)
{
this.start = start;
this.stop = stop;

this.cntDepth = start.length;

if (doCnt)
{
this.execute();
}
}

public abstract boolean execute(int[] cnt);

public void execute()
{
this.doCnt(new LinkedList());
}

public void doCnt(LinkedList cnt)
{
int doCntDepth = cnt.size();

if (doCntDepth < this.cntDepth)
{
for (int cntUp = start[doCntDepth]; cntUp < stop[doCntDepth]; cntUp++)
{
cnt.add(new Integer(cntUp));
this.doCnt(cnt);
cnt.removeLast();
}
}
else
{
this.execute(cnt);
}
}

public boolean execute(LinkedList cnt)
{
return this.execute(Cnt.convertToIntArray(cnt));
}

public static int[] convertToIntArray(final LinkedList intList)
{
final int intArray[] = new int[intList.size()];

for (int cnt = intList.size() - 1; cnt >= 0; cnt--)
{
intArray[cnt] = ((Integer) intList.get(cnt)).intValue();
}

return intArray;
}
}

The console output is:

Test by for-cycle, referring to objects of method-block
false
0
first
1
second
2
last
Test by Cnt-Instance, referring to objects of method-block, that this works is my main target
0
Test by Cnt-Instance, referring to objects of Class Cnt
0
first
1
second
2
last
false
0
java.lang.NullPointerException
at a0hirz.multiproject.cyclic.Cnt$1.execute(Cnt.java:47)
at a0hirz.multiproject.cyclic.Cnt.execute(Cnt.java:212)
at a0hirz.multiproject.cyclic.Cnt.doCnt(Cnt.java:206)
at a0hirz.multiproject.cyclic.Cnt.doCnt(Cnt.java:200)
at a0hirz.multiproject.cyclic.Cnt.execute(Cnt.java:188)
at a0hirz.multiproject.cyclic.Cnt.init(Cnt.java:180)
at a0hirz.multiproject.cyclic.Cnt.<init>(Cnt.java:158)
at a0hirz.multiproject.cyclic.Cnt.<init>(Cnt.java:147)
at a0hirz.multiproject.cyclic.Cnt$1.<init>(Cnt.java:36)
at a0hirz.multiproject.cyclic.Cnt.main(Cnt.java:36)
java.lang.NullPointerException
at a0hirz.multiproject.cyclic.Cnt$3.execute(Cnt.java:102)
at a0hirz.multiproject.cyclic.Cnt.execute(Cnt.java:212)
at a0hirz.multiproject.cyclic.Cnt.doCnt(Cnt.java:206)
at a0hirz.multiproject.cyclic.Cnt.doCnt(Cnt.java:200)
at a0hirz.multiproject.cyclic.Cnt.execute(Cnt.java:188)
at a0hirz.multiproject.cyclic.Cnt.init(Cnt.java:180)
at a0hirz.multiproject.cyclic.Cnt.<init>(Cnt.java:158)
at a0hirz.multiproject.cyclic.Cnt.<init>(Cnt.java:147)
at a0hirz.multiproject.cyclic.Cnt$3.<init>(Cnt.java:87)
at a0hirz.multiproject.cyclic.Cnt.main(Cnt.java:87)



 Auf diesen Beitrag antworten
 Neuen Beitrag verfassen


|<   <   >   >|

                                                                                                                                                                                                                           

Impressum  · Datenschutz  · AGB  · Infos  · Presse
Ein modernes Forum: teamturn.com