This question already has answers here:
Java inner class and static nested class
(28 answers)
Closed 5 years ago.
How can i create a instance of the following Class and access its methods.
Example:
public class A {
public static class B {
public static class C {
public static class D {
public static class E {
public void methodA() {}
public void methodB(){}
}
}
}
}
}
You can use :
A.B.C.D.E e = new A.B.C.D.E();//create an instance of class E
e.methodA();//call methodA
e.methodB();//call methodB
Or like #Andreas mention in comment you can use import A.B.C.D.E;, so if your class is in another packager then you can call your class using name_of_package.A.B.C.D.E like this:
import com.test.A.B.C.D.E;
// ^^^^^^^^------------------------name of package
public class Test {
public static void main(String[] args) {
E e = new E();
e.methodA();
e.methodB();
}
}
Related
what is the way in ASM visitors to figure out the class type of the class containing a static method.
My code looks like
public static class A {
public void a() {
B.b();
}
}
public static class B {
public static void b() {
}
}
So I want when visiting class A to be noticed that class B is being used
This question already has answers here:
Curly braces in "new" expression? (e.g. "new MyClass() { ... }")
(3 answers)
Closed 4 years ago.
we know in java that you cant create an instance of an abstract class.
but, this works:
public abstract class MyAbstract
{
int num = 10;
}
//and in main class
public static void main(String[] args) {
MyAbstract abstractObject = new MyAbstract() {};
System.out.println(abstractObject.num);
}
output:
run:
10
So we can ?
In short what is happening here ?
The syntax
new X(...) { ... }
instantiates an ad-hoc, anonymous class, extending / implementing the class / interface named X.
In your case, the abstract class has all it needs, there are no abstract methods. Therefore, the derived class doesn't need to implement any missing methods.
If you add an abstract method to your abstract class, the example will no longer work. To make it work again, you will have to implement the method in the { ... } section.
public abstract class MyAbstract
{
int num = 10;
abstract void f();
}
public static void main(String[] args) {
MyAbstract abstractObject = new MyAbstract() {
void f() {
...
}
};
System.out.println(abstractObject.num);
}
In addition, you can pass arguments to an extended base class's constructor by using the () part of the syntax:
public abstract class MyAbstract
{
MyAbstract(int argument) {
...
}
int num = 10;
abstract void f();
}
public static void main(String[] args) {
MyAbstract abstractObject = new MyAbstract(5) {
void f() {
...
}
};
System.out.println(abstractObject.num);
}
This question already has answers here:
How to determine an object's class?
(13 answers)
Closed 5 years ago.
Following super-class/sub-class relationship:
public abstract class A {
public Class<? extends A> getSubClass() {
Class<? extends A> clazz = ???
}
}
public class B extends A {
/* some implementation*/
}
public class Foo {
public static void main(String... args) {
A a = new B();
a.getSubClass();
}
}
Is there a way that b.getSubClass()returns the acutall sub-class type?
This
package test;
public class SubClass {
public static void main(String[] args) {
final A b = new B();
System.out.println("Class: " + b.getSubClass().getName());
}
public static abstract class A {
public Class<? extends A> getSubClass() {
return this.getClass();
}
}
public static class B extends A {
// No further implementation
}
}
produces
Class: test.SubClass$B
Even in super-class methods, the actual class is the sub-class.
This question already has answers here:
How to access java-classes in the default-package?
(5 answers)
Closed 8 years ago.
I write a normal class
public class TestAccess {
}
class T{
private int i=0;
int j=0;
protected int k=0;
public int m=0;
}
class TT
{
public void m(){
T t= new T();
t.j=9;
}
}
Then I access it in the same directory with another class
public class TestProtected extends T
{
public void method(){
System.out.println(k);
}
public static void main(String[] args)
{
System.out.println("HW!");
}
}
But when I use package in the second class, there is something wrong to access class T;
package m;
public class TestProtected extends T
{
public void method(){
System.out.println(k);
}
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}
Can you guys tell me how I can access class T in package m?
The issue here is not related with public, I have tried, to be more general, what if you only have class T instead of its java code? The situation is you can access the T class without package, but you cannot access it with package.
You should make your class T public. Otherwise, it is not visible to classes in different packages.
You'll have to move T to a separate file, since you can't have multiple top level public classes in the same file.
Finally, your TestProtected class located in pacakge m should import the T class.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Create instance of generic type in Java?
I got these classes
public abstract class Base {
public String Id = "originalId";
}
public class InstanceOfBase extends Base {
public void setString(String test) {
this.Id = test;
}
}
public class UseIt {
public Test<InstanceOfBase> test = new Test<InstanceOfBase>();
public void run() {
InstanceOfBase instanceOfBase = test.createMe();
System.out.println(instanceOfBase.Id);
}
}
public abstract class Test<E extends Base> {
public E createMe() {
// How do I do this?
return new E();
}
}
The code above does not compile because it does not know how to create E. how can I achieve this?
When I invoke the run method, I expect it should print "originalId".
Unfortunately you cannot create classes from generic types in java. But you can do as Justin Rudd suggested in this thread and write:
public E createMe(Class<E> clazz)
{
return clazz.newInstance();
}
So you use the class archetype to create a new instance. Invoking it could be:
InstanceOfBase instanceOfBase = test.createMe(test.getClass)