How to Pass Parameters to Main Method from another class? - java

I have class that contains only a main method. I want to pass parameters to that method not from the terminal but from another class. How Can i do this?
public class class1
{
public class1{}
}
public class class2
{
public class void main(String args[]){}
}
I want to pass An object of class class1 to the main of class2 . is That Possible??

You can call it as usual.
Example :
public class Test {
public static void main(String[] args) {
System.out.println("Inside Test class main()");
}
}
The other class
public class Test2{
public static void main(String args[]) {
System.out.println("Inside Test2 class main()");
Test.main(args);
}
}
Now run the Test2 class.
Output:
Inside Test2 class main()
Inside Test class main()

There is a reason why they call it the main method. It is the first method that is executed when your application starts. And you cannot call it (as the main method), the JVM does it for you.
Of course you can call it like any other method, but it will not be called in the role of the main method but as any other ordinary static method.

Related

Error in Intreface program in Java: Could not find or load main class interface

This message is shown in an online compiler
Error: You must declared Main class which contains 'main' method, which is entry point of program execution.
interface I1{
void show();
}
class Test implements I1{
public void show(){
System.out.println("Hi");
}
public static void main(String[] args){
Test t = new Test();
t.show();
}
}
You need to have main method in a public class
your 'non public class main method' won't work as an entry point of your code.
class Test implements I1{ to public class Test implements I1 {
You need at least one public static void main in a public class ??? (you fill the ???), you can have more, but you can't have none.

How to test a method using Eclipse? (Java)

So i have coded a method on eclipse (java), and I want to test if it works correctly, how do I do this, because the program won't run unless it has a main header.
So I guess what im asking is how do i use a method in another code
Well if your method is static you can access it via it's class name, if its a member of the class you have to create a instance of the class and call it using the instance.
Let's say we have this class and we want to call both methods in another class:
public class ClassToTest {
public static void staticMethodToTest(){
//Some code
}
public void memberMethodToTest(){
//Some code
}
}
To test them you can create another class:
public class MyClass {
//Create a main method so you can run your code
public static void main(String[] args) {
//Call static method
ClassToTest.staticMethodToTest();
//Call member
//Create instance of class
ClassToTest classToTestInstance = new ClassToTest();
//Call method on instance
classToTestInstance.memberMethodToTest();
}
}
In the case that both classes are in different packages you have to import the ClassToTest using import package.name.ClassToTest;
I think you want to test the internal workings of your program, you can do this by either creating a temporary main() method, or by using JUnit testing.
You can accomplish this simply by doing:
public static void main(String[] args)
{
//Test it here
}
and you should be able to just comment it out/remove it if it works.
//Have the method declared inside a class.
public MyClass(){
public String myString(String m){
return m;
}
}
//Create another class for your main method and inherit from MyClass
public MainClass extends MyClass {
public static void main(String[] args){
//Create an object obj from MyClass()
//and call the method on the object
MyClass obj = new MyClass();
System.out.println(obj.myString("hello"));
}
}
Write the method in another class, and give it a constructor so that it can be instantiated. In your main class, make an instance of the class and call your method from it.

Groovy - Main method placement

Here is my code:
class cat {}
class dog {
static void main(String[] args) {}
}
When compiled groovy says I do not have a main method. But when I get rid of the cat class:
class dog {
static void main(String[] args) {}
}
Its valid. I thought, as long as I had the main method in any class the code was valid, but I am wrong. Can someone explain why I can not have more than one class when the main method resides in one of the classes?
You can have more than one class, but the class defined first has to have the main method implementation. Normally when run as a script, the script is executed in run() method.
In case you have a class defined, then the name of the class is used as the name of the script. In case there are more than one public class, then the runnable implementation has to be part of the first defined class. Below should work:
class Dog {
static void main(String[] args) {
println "hello"
}
}
class Cat {}
You can get a clear picture when you inspect AST in groovy console.

Instances of classes

Does a non-static method create an instance of the class in which it is declared? If not why this code works?
import java.awt.*;
public class Main extends Frame {
public Main () {
//super keyword needs an istance of the class don't it?
super ("My frame");
super.setSize (200,100);
}
public static void main (String [ ] args) {
new Main();
}
}
If a non-static method creates an instance of the class the next code should work...
import.java.applet.*;
public class Main extends Applet {
public void print () {
System.out.println ("Hi");
}
public void init () {
this.print();
}
}
A non-static method can only be accessed in the context of an instance that already exists.
public class Foo {
public static void someStaticMethod() { /* ... */ }
public void someNonStaticMethod() { /* ... */ }
}
Elsewhere:
Foo.someStaticMethod(); // this works
Foo.someNonStaticMethod(); // this DOESN'T work
Foo foo = new Foo();
foo.someNonStaticMethod(); // but this does
Within a non-static method, you have access to an instance by default (implicitly), or can refer to it explicitly using the this keyword. In your example:
public class Main extends Frame {
public Main () {
//super keyword needs an istance of the class don't it?
super ("My frame");
super.setSize (200,100);
}
public static void main (String [ ] args) {
new Main();
}
}
...the instance in question in the call to super is the implicit instance you create with new Main().
Instances of classes in Java are created by calling a constructor using the new keyword:
Main main = new Main();
public void Main () { } however is not a constructor, but a instance method (which, in your example, never gets called).
public class Main {
public static void main(String[] args) {
Main main = new Main(); // create instance
main.Main(); // call method 'Main'
new Main().Main(); // or both at once
}
public Main() {
// this is the (default) constructor
}
public void Main() {
// this is an instance method (whose name 'should' start lowercase
}
}
Does a non-static method create an instance of the class in which it is declared?
No.
why this code works?
Because when such a method is called, it should be called from an already instantiated instance. "this"/"super" refer to the implicit parameter, which is the instance in question (instantiated and passed in from elsewhere, with "new"). If instantiation has not occurred, a NullPointerException will immediately be thrown.
Well actually in the main question I wrote a wrong code (I've been out for all the day and I'm quite tired) anyway it helped me to understand more. Now what about this code
import java.applet.*;
public class Main extends Applet {
//overrides Applet.init ()
public void init () {
//here I use super keyword without creating an istance of the class
super.init ();
//code here...
}
}

Call parent method from an instance

I was looking at this topic:
java Access parent method from imported child class
But I'm still not sure the proper terminology for what I'm trying to do.
I have an Instance of Test and I want to call a method from the "parent" that created the instance.
public class Main {
public Main {
Test test1 = new Test();
}
public void showMessage(String message) {
System.out.println(message);
}
}
public class Test {
public Test {
//how do I call Main.showMessage("test is running")?
}
}
The answer in the topic I listed above was:
Assuming the "parent" is a class you're extending and the method you're calling is NOT static, the following should do the trick:
super.toggleVisibility();
If it's a static method - it's even Simpler:
Main.showMessage();
The issue:
I'm pretty sure I can't use super() cause I'm not extending a class. And I'm not sure if Main.showMessage(); will work because I haven't referenced the parent Main within the Test class.
You can pass an instance of Main into Test, and I'd use an interface to help decouple things:
public class Main implements Parent {
public Main() {
Test2 test2 = new Test2(this);
}
public void showMessage(String message) {
System.out.println(message);
}
public static void main(String[] args) {
new Main();
}
}
interface Parent {
void showMessage(String message);
}
class Test2 {
public Test2(Parent parent) {
parent.showMessage("I am running from in Test");
}
}
Create an instance of the desired class and call the desired method:
public class Test {
public Test() {
//how do I call Main.showMessage("test is running")?
Main main = new Main();
main.showMessage("test is running");
}
}
As noted by #BrianRoach, by your current code, this will generate a StackOverflowError since it is an infinite loop (Main instance that creates a Test instance in its constructor that creates a Test instance in its constructor ...)
So, another option may be passing a Main class instance to Test constructor:
public class Test {
public Test(Main main) {
main.showMessage("test is running");
}
}
Then, in Main constructor:
public Main() {
Test test1 = new Test(this);
}

Categories

Resources