public class FirstClass
{
private FirstClass()
{
new Thread(new Thread()).start();
}
public static void checkSomething(FirstClass clas)
{
//doing something
}
private class Thread implements Runnable
{
#Override
public void run() {
checkSomething(????);
}
}
public static void main(String[] args)
{
new FirstClass();
}
}
my question is, what to write in ???? to get class FirstClass, i cannot write "this", coz i would get Thread.
You can write FirstClass.this to access the enclosing class instance.
Related
I would like to know if it is possible from a child class to get caller attributes. Follow a sample.
TestClass1.class
package test;
public class TestClass1 {
private String myTestClass1Var = "xxxxx";
public static void main(String[] args) {
new TestClass1().class2();
}
private void class2() {
new TestClass2().getClass1Attributes();
}
}
TestClass2.class
package test;
public class TestClass2 {
public void getClass1Attributes() {
//is something like this possible?
getCaller().myTestClass1Var
}
}
Thanks
Interface :
public interface MyFirstInterface {
void myFirstAbstractMethod();
default void myDefaultMethod() {
System.out.println("Hi I am default method in Interface.");
}
}
Class:
public class MyFirstClass{
public static void main(String[] args) {
MyFirstInterface myFirstInterface = new MyFirstInterface() {
#Override
public void myMethod() {
System.out.println("Main class : "+this.getClass());
}
};
myFirstInterface.myMethod();
myFirstInterface.defaultMethod();
}
}
Now we know we are instantiating a anonymous class, what I want to know is Why would anyone use it? What is the advantage or disadvantage of doing it?
It is not a constructor. It is a method a() declaration with the return type A.
Also, we can instantiate an interface using an anonymous class. Runnable for example:
Runnable r = new Runnable() {
#Override
public void run() { ... }
};
Runnable r = () -> { ... }
interface Sporty {
public void beSporty();
}
class Ferrari implements Sporty {
public void beSporty() {
System.out.println("inside Ferrari impelemnting Sporty");
}
}
class RacingFlats implements Sporty {
public void beSporty() {
System.out.println("inside RacingFlats impelemnting Sporty");
}
}
public class TestSportythings {
public static void main(String[] args) {
Sporty[] sportyThings = new Sporty[3];
sportyThings[0] = new Ferrari();
sportyThings[1] = new RacingFlats();
}
}
You can call methods from Array instance of the interface by an object of a class which is implemented that method of the interface.
Like the following:
sportyThings[0].beSporty();
And you will get output:
inside Ferrari impelemnting Sporty
But if you call beSporty() by
sportyThings[2].beSporty();
You will get NullPointerException as sportyThings[2] is not initialized (by new).
Can Someone tell me with an example why an class should be defined inside an interface.
The below is the simple code i was trying.
interface Watsapp
{
class A
{
public void Validate()
{
}
};
abstract public void SendText();
public void SendPic();
};
its totally depends on logic requirements.
whenever we declare inner class, it treats as a data member so here also you can treat this class as a data member
just assume scenario some one needs object of A inside Interface and there is no class right now.
see eg.
public interface Watsapp
{
class A
{
public void Validate()
{
}
public String iDoSomething()
{
return "i did";
}
};
public A objOfA = new A();
abstract public void SendText();
public void SendPic();
};
And main Class is bellow:
public class TestMain {
public static void main(String[] str){
System.out.println( Watsapp.objOfA.iDoSomething());
}
}
mostly people create anonymous class for one time use, but here You created a class with name.
see:
public interface Watsapp
{
/*class A
{
public void Validate()
{
}
public String iDoSomething()
{
return "i did";
}
};*/
Thread t = new Thread()
{
public void run() {
// something ...
}
};
abstract public void SendText();
public void SendPic();
};
Thank you.
I need to write some importers. They need all the same initialization. So I try to write an abstract class, which does all the initialization and also has the main method, so that all sub-classes just need to implement run() to do their specific import work:
public abstract class AbstractImporter {
public AbstractImporter() {
// Initialization
}
public abstract void run();
public static void main(String[] args) {
AbstractImporter importer = new AbstractImporter();
importer.run();
}
}
public class ConcreteClass() {
public void run() {
// Do some importing
}
}
Of course it fails to create an instance of this abstract class (new AbstractImporter()).
Does anybody has any idea how to solve that? TIA!
Obviously you need a concrete class - anonymous or otherwise.
Better to move the main method to another class and instantiate the appropriate concrete class based on data (either your domain specific or a constant) and then run it. This way each implementation can be independent of other implementations.
public abstract class AbstractImporter {
public AbstractImporter() {
// Initialization
}
public abstract void run();
}
public class ConcreteImporter1 extends AbstractImporter {
public void run() {
//do something
}
}
public class ImporterMain() {
public static void main(String[] args) {
AbstractImporter importer = createImporter(args[1]);
importer.run();
}
private static AbstractImporter createImporter(String type) {
if (type.equals("1")) {
return new ConcreteImporter1();
}
....
}
}
new AbstracterImporter() {
public void run() {
// ...
}
};
I apologize for current lack of formatting, currently on a mobile device.
public abstract class AbstractImporter {
public AbstractImporter() {
// Initialization
}
public abstract void run();
public static void main(String[] args) {
AbstractImporter importer = new AbstractImporter(){
public void run() {
System.out.println("Anonymous implementation");
}
};
importer.run();
}
}
You cannot create an instance of an abstract class.
public abstract class AbstractImporter {
public AbstractImporter() {
// Initialization
}
public abstract void run();
}
public class ConcreteClass extends AbstractImporter{
public void run(){
//Implementation
}
public static void main(String args[]){
AbstractImporter ai = new ConcreteClass();
ai.run();
}
}