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.
Related
In the same project I have two packages, 1st package contains a class with this code:
package com.ginger;
public class SimplePrint
{
public SimplePrint(){}
public static void print(Object obj)
{
System.out.println(obj);
}
}
I would like use the method print() in another class in another package, but within the same project.
import com.ginger.*;
public class MainClass
{
public static void main(String[] args)
{
print("Some");
}
}
But the compiler tells me that the method print() is undefined for the 2nd class.
In the same time, I am able to create the object SimplePrint s = new SimplePrint() in the 2nd class.
I'm new to the programming, excuse me if i am asking about the simple thing.
There are a couple of ways to do this:
Non-static
Remove the static keyword of the method print and create an instance of the class
SimplePrint simplePrint = new SimplePrint();
and just do this
simplePrint.print("");
Or combine the above into a single line:
new SimplePrint().print("");
Static
You keep the print method static and just do this
SimplePrint.print("");
static keyword for a method implies its a class level.
If keyword like static is not used for a method it means instance level,so create instance and then access it.
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.
In the same project I have two packages, 1st package contains a class with this code:
package com.ginger;
public class SimplePrint
{
public SimplePrint(){}
public static void print(Object obj)
{
System.out.println(obj);
}
}
I would like use the method print() in another class in another package, but within the same project.
import com.ginger.*;
public class MainClass
{
public static void main(String[] args)
{
print("Some");
}
}
But the compiler tells me that the method print() is undefined for the 2nd class.
In the same time, I am able to create the object SimplePrint s = new SimplePrint() in the 2nd class.
I'm new to the programming, excuse me if i am asking about the simple thing.
There are a couple of ways to do this:
Non-static
Remove the static keyword of the method print and create an instance of the class
SimplePrint simplePrint = new SimplePrint();
and just do this
simplePrint.print("");
Or combine the above into a single line:
new SimplePrint().print("");
Static
You keep the print method static and just do this
SimplePrint.print("");
static keyword for a method implies its a class level.
If keyword like static is not used for a method it means instance level,so create instance and then access it.
Hi i have a Base class containing one string member as belows :
public class BaseClass
{
public String test;
}
Child class extending base class where i wish to initialize the test value.
public class ChildClass extends BaseClass
{
public void initialize()
{
System.out.println("inside constructor of ChildClass.");
this.test="stringtest";
}
}
Test class where i wish to use the value of test variable of base class:
public class TestClass extends BaseClass
{
public void test()
{
new ChildClass().initialize();
System.out.println(this.test);
}
public static void main(String[] args) {
new TestClass().test();
}
}
Now my above code is printing null inside test class. why so? although i have initialized the test variable in child class? am i going wrong somewhere in java concepts?
the problem is that you create new ChildClass but you aren't setting it in a variable. then you print this.test which is never set.
when you are in test method you are in TestClass instance:
you are creating and setting a ChildClass class
but then you are printing the TestClass test member
if you just want to create ChildClass and use it do
public class TestClass
{
public void test()
{
ChildClass cls =new ChildClass().initialize();
System.out.println(cls.test);
}
public static void main(String[] args) {
new TestClass().test();
}
}
or if you want to extend ChildClass do
public class TestClass extends BaseClass
{
public void test()
{
initialize();
System.out.println(this.test);
}
public static void main(String[] args) {
new TestClass().test();
}
}
new ChildClass() and new TestClass() are 2 different objects even if they are extending common BaseClass.
Having a common BaseClass as super class doesn't mean that it shares the non static fields of it, with all the instances of its different subclasses
This would have worked if test was static (shared class field) in BaseClass
There are two instances of BaseClass in the example you posted. One is the one instantiated with new ChildClass() and the other one is instantiated by the main() method (TestClass). Each one of them, being a subclass of BaseClass, has its own test member (they are different variables with different values).
Remember that the this keyword always references the instance in which it is used.
In this case, System.out.println(this.test); is accessing the test property of the TestClass instance created in the main method.
You need to access the test property of the other instance. You could do so by keeping a reference to the ChildClass instance and accessing the test property afterwards:
ChildClass instance = new ChildClass().initialize();
System.out.println(instance.test);
You might find the following Java Tutorials page useful: Using the this Keyword.
Also take into account that TestClass doesn't need at all to extend BaseClass. You could keep accessing instance.test because it is a public member, but you should consider making the field private and provide getter and setter methods. See the following question for relevant information on this: Why use getters and setters?
You need a instance of Child Class
ChildClass cls =new ChildClass().initialize();
System.out.println(cls.test);
this is referring to the Test Class instance.
If static methods are resolved at compile time how is an object instance able to call a static method?
class StaticCall
{
public static void main(String[] args)
{
String arr[]={"Class StaticCall","calls static method of class MyMainClass"};
MyMainClass h=new MyMainClass();
h.main(arr); //How is an instance able to call a static method?
System.out.println("this is StaticCall main");
}
}
class MyMainClass
{
public static void main(String[] args){
System.out.println(args[0]+" "+ args[1]);
}
}
After running the StaticCall class the output is
Class StaticCall calls static method of class MyMainClass
this is StaticCall main
As static fields and methods belong to the Class object how is an instance able to call a static method?
Also when is the Class object created,Is it on first access to any of it's fields or methods?
How is an instance able to call a static method?
It doesn't. Try this instead
MyMainClass h = null;
h.main(arr);
and you will see that the instance is ignored as this is exactly the same as
MyMainClass.main(arr);
To extend your example ... if you have
class AnotherMainClass extends MyMainClass
{
}
then all the following call the same method.
AnotherMainClass amc = null;
amc.main(args);
((AnotherMainClass) null).main(args);
AnotherMainClass.main(args);
MyMainClass mmc = null;
mmc.main(args);
((MyMainClass) null).main(args);
MyMainClass.main(args);
h.main(arr); //How is an instance able to call a static method?
This is just a shortcut for MyMainClass.main(arr), i.e. the static type of h. The usage is often frowned upon and most IDEs will recommend you use the type instead of instance.
Since this occurs at compile time, h can be null
you can call static method by classname.staticMethod and even instance.staticMethod, instance.staticMethod internally call classname.staticMethod.