How can I wrap main in an extendable class? - java

I want to make a test program that instead of specifying main method explicitly, extends a class/abstract class and overrides a method that gets called by that superclass eg init.
My attempt:
JavaApplication.java
public class JavaApplication {
public JavaApplication(){
this.init(null);
}
public JavaApplication(String[] args) {
this.init(args);
}
public void init(String[] args) {
/* override me */
}
public static void main(String[] args) {
new JavaApplication(args);
}
}
MyApp.java:
public class MyApp extends JavaApplication {
#Override
public void init(String[] args) {
System.out.println("Hello, World!");
}
}
The code compiles but my init method is not called(The string does not appear).
What is the proper way of formulating this behavior in Java?
Related Questions:
can-i-access-a-subclass-method-through-a-base-class-typed-reference
calling-a-method-of-subclass-through-reflection
whats-wrong-with-overridable-method-calls-in-constructors
calling-an-overridden-method-from-a-parent-class-ctor

You need to add a main method in MyApp that creates an instance of MyApp:
public static void main(String[] args) {
new MyApp(args);
}
Then, run MyApp instead of JavaApplication.

Related

How a static method call works in Java?

When calling out the function testFunc(), I am not using the syntax
Apples.testFunc(). Yet the code runs successfully. How so?
class Apples {
public static void main(String args[] ) {
testFunc();
}
public static void testFunc() {
System.out.println("Hello world!");
}
}
Since, the static method is in the same class. So, you don't need to specify the class name.
If it is in different class, then you need to specify the class name.
Remember: Non-static methods can access both static and non-static members whereas static methods can access only static members.
For example :
Calling a static method present in different class, you need to do like this :
import com.example.Test;
public class Apples {
public static void main(String args[]) {
Test.testFunc();
}
}
package com.example;
public class Test {
public static void testFunc() {
System.out.println("Hello world!");
}
}
Your function testFunc() is in same class where the function main is. So you don't need to use class name before function testFunc().
When the memory is allocated for the class, Static method or static variable is assigned memory within a class. So we can access static member function or data variable using class name.
we can call static function as below
class Main1 {
public static void main(String args[] ) {
Main1.testFunc();
}
public static void testFunc() {
System.out.println("Hello world!");
}
}
or
class Main1 {
public static void main(String args[] ) {
testFunc();
}
public static void testFunc() {
System.out.println("Hello world!");
}
}
the answer will be same, but when static function is in other class then we must use classname to call it

How do I run the main method in a different file in Java?

package jav;
class PackageDemo
{
public void display()
{
System.out.println("PackageDemo executed");
}
}
public class PackageDemoDriver
{
public static void main(String[] args) {
PackageDemo boy = new PackageDemo();
boy.display();
}
}
This is the code for a package.
I will be importing this package into a different file.
The code for that is:
package exercise;
import jav.PackageDemoDriver;
class Exe
{
public static void main(String[] args) {
}
}
What should I fill in the main method to run display(), if it is possible to do so?
You can run static methods as needed
public static void main(String[] args) {
PackageDemoDriver.main(args);
}
Based off your question I assume you want the main function in Exe to essentially run the main function in PackageDemoDriver:
public class Exe {
public static void main(String args[]) {
PackageDemoDriver.main(null);
}
}
I think that'll provide the functionality you're after, if your PackageDemo and PackageDemoDriver are in different classes
Is this what you're looking for?
package exercise;
import jav.PackageDemoDriver;
class Exe {
public static void main(String[] args) {
PackageDemoDrive.main(args);
}
}

How to execute a java nippet from another one

I have the following classic java script.
public class HelloWorld3 {
public static void main(String[] args) {
System.out.println("Hello");
}
}
I just want to run this script from another script so that it just prints out "Hello"
this was my attempt to do just that.
public class Test {
public static void main(String[] args) {
HelloWorld3 obj = new HelloWorld3();
System.out.println(obj);
}
}
which failed and I get why it failed.
Not sure how to do it right though.
The main method is used to start a program, there is no need to have it in two different classes and to call one from the other.
To construct an instance of a class you need a constructor. Here's one:
public class HelloWorld3 {
public HelloWorld3(){
}
}
Now you can call new HelloWorld3 from another class (such as Test) to create a HelloWorld3 type object. If you want this object to print a message, lets add a method to it:
public class HelloWorld3 {
public HelloWorld3(){
}
public void printHello(){
System.out.println("Hello");
}
}
You may now use this main method inside your Test class:
public static void main(String[] args) {
HelloWorld3 obj = new HelloWorld3();
obj.printHello();
}
Modify the HelloWorld3
public class HelloWorld3 {
public static void main(String[] args) {
System.out.println("Hello");
}
public void printMeStatic(String msg) {
System.out.println(msg);
}
public void printMeInstace(String msg) {
System.out.println(msg);
}
}
Use it in other class
public static void main(String[] args) {
HelloWorld3 obj = new HelloWorld3();
System.out.println(obj.printMeInstace("a msg 1")); //use this if you need an instance/ object of the class HelloWorld3
System.out.println(HelloWorld3.printMeStatic("a msg 2")); //use this for static methods (you dont need an object to use them)
}

How to call a method defined while object instantiation?

wondering how it is possible to call public m method?
public class Test1 {
public static void main(String[] args) {
Test1 test = new Test1() {
public void m() {
System.out.println("m");
}
};
}
}
I don't believe you can. You'd have to create an interface or subclass. (Well, okay, that's probably not true. You could probably do it with reflection.)
E.g., like this (where you call it via test.m() after construction):
public class Test1 {
public static void main(String[] args) {
SubTest1 test = new SubTest1() {
public void m() {
System.out.println("m");
}
};
test.m();
}
private static abstract class SubTest1 extends Test1 {
public abstract void m();
}
}
Or like this, where it happens during construction:
public class Test1 {
public static void main(String[] args) {
SubTest1 test = new SubTest1() {
public void m() {
System.out.println("m");
}
};
}
private static abstract class SubTest1 extends Test1 {
public SubTest1() {
this.m();
}
public abstract void m();
}
}
You can't define an anonymous class constructor, so that last uses the constructor of the SubTest1 class and the abstract method.
You cannot directly invoke m since test is of type Test1 which does not contain a method called m, but you should never find yourself in a situation like this. The whole point of anonymous classes is to alter some already-existent aspect of the base class's functionality, so adding new methods makes no sense. Consider rethinking your design or using a named class instead.
Of course, if you won't care about test in the future you could do this:
new Test1() {
public void m() {
System.out.println("m");
}
}.m();
Although you would rarely want to do something like this, it could be useful if you're working with Thread or Runnable and need to invoke the run method.
If Test1 had a method called "m" you could just call test.m() after you instantiated the inner class:
public class Test1 {
public static void main(String[] args) {
Test1 test = new Test1() {
public void m() {
System.out.println("New Behavior");
}
};
test.m();
}
public void m() {
System.out.println ("Default Behavior");
}
}
Running this would output:
New Behavior

Call an overridden method in my main class

My code is like this...
but there seems to be a problem when I call the overridden method createHome(). Here is a sample code:
public class Test extends SweetHome3D {
public static void main(String [] args) {
new Test().init(args);
***createHome();***
}
#Override
public Home createHome() {
Home home = super.createHome();
// Modify home as you wish here
return home;
}
}
I take it that code didn't compile? You are calling createHome() as if it's a static method.
public static void main(String [] args) {
Test test = new Test();
test.init(args);
test.createHome();
}

Categories