How to execute a java nippet from another one - java

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)
}

Related

How to create an object of a class that lives within a method in Java?

I have following class:
public class SomeClass {
public static void main(String[] args) {
//
}
private static void test() {
MyClass myClass = new MyClass(); //Doesn't work
myClass.print(); //Doesn't work
class MyClass {
void print() {
System.out.println("Some text");
}
}
}
}
How to create an object of type MyClass? Both lines produce compilation error.
You need to declare the class first before being able to use it:
public class SomeClass {
public static void main(String[] args) {
test();
}
private static void test() {
class MyClass {
void print() {
System.out.println("Some text");
}
}
MyClass myClass = new MyClass();
myClass.print();
}
If you are using Java version before then 11 then you can't define class inside method and you need to try to define it in scope of SomeClass and then access it through the SomeClass:
public class SomeClass {
public static void main(String[] args) {
test();
}
private static void test() {
MyClass myClass = new SomeClass().new MyClass(); // Doesn't work
myClass.print(); // Doesn't work
}
class MyClass {
void print() {
System.out.println("Some text");
}
}
}
However, if you are using Java 11+ you need to change the order of your code inside the test method to define the class first and access it next:
public class SomeClass {
public static void main(String[] args) {
test();
}
private static void test() {
class MyClass {
void print() {
System.out.println("Some text");
}
}
MyClass myClass = new MyClass(); // Doesn't work
myClass.print(); // Doesn't work
}
}

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 can I wrap main in an extendable class?

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.

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