I'm creating a project that need to pass string from first program to second program but i need to pass string in main method of first class. I've googled but i cannot find what i need, mostly people use setter and getter to pass string between class but i cannot do that in main method.
How to pass string in main method of another class?
what i need is shown here:
public class FirstProgram{
public void first(){
String a = "hello";
}
}
public class SecondProgram{
public static void main(String[] args){
//i need to pass string here
}
}
Its not a good approach to take, but technically you can call the main method of the Second program and pass whatever argument you like
public class FirstProgram{
public void first(){
String a = "hello";
SecondProgram.main(new String []{a});
}
}
You can do this:
First.java:
public class First {
public static void main(String[] args) {
Second.main(args);
}
}
Second.java:
import java.util.Arrays;
public class Second {
public static void main(String[] args) {
Arrays.stream(args).forEach(System.out::println);
}
}
If you execute java First foo bar baz bat on the command line, you'll see that Second prints out "foo bar baz bat" in the console.
I don't recommend it.
You have to start your app by calling main in some program. Your First can invoke main in Second as shown, but it's First that starts the process.
Pretty confusing question.
But you can do smth like that:
public class FirstProgram{
public String first(){
String a = "hello";
return a;
}
}
public class SecondProgram{
public static void main(String[] args){
FirstProgram firstProgram = new FirstProgram();
String result = firstProgram.first();
}
}
Related
(This is a repost of a previous question I have where I modified the code so it is much easier to understand.)
public class LaptopSaleApp{
public static void main(String[] args){
selectLaptop();
selectSupportOptions(laptopOrder);//This parameter cannot be resolved into a variable
}
public static void selectLaptop(){
LaptopSalesOrder laptopOrder = new LaptopSalesOrder("Dell Latitude", 1399.00);
}
public static void selectSupportOptions(LaptopSalesOrder laptopOrder){
laptopOrder.setBasicIndicator(true);
}
}
I need two static methods here. The object has to be declared in the first method, and then is used in the second method for the setBasicIndicator setter. I then need to call both methods in my main to run the program, but I don't know what to put in the parameters for the method in the main.
You can't quite do this if the object is declared in the first method.
Here's what you should do:
public static void main(String[] args){
LaptopSalesOrder laptopOrder = selectLaptop();
selectSupportOptions(laptopOrder);
}
public static LaptopSalesOrder selectLaptop(){
return new LaptopSalesOrder("Dell Latitude", 1399.00);
}
public static void selectSupportOptions(LaptopSalesOrder laptopOrder){
laptopOrder.setBasicIndicator(true);
}
I've been flustered over trying to figure out how to call a method from an instance of a class from a different class. For example:
public class Test1
{
public static void makeSomeInst()
{
Test1 inst = new Test1();
inst.fireTest();
}
public void fireTest()
{
System.out.println("fireTest");
}
public Test1()
{
}
}
no problem with understanding the above, but what If I want to do something to inst from a class called Test2, how would I do that? The below example doesn't work:
public class Test2
{
public static void main(String[] args)
{
Test1.makeSomeInst();
inst.fireTest();
}
}
And just to be extra clear, I get that I can call static references without instantiating, but I just want to know, in this specific case
What is the syntax to reference the test1 object called inst from the Test2 class?
what If I want to do something to inst from a class called Test2, how would I do that?
First of all you have to teach the Test2 class what Test1 is.
public class Test2
{
public doSomething()
{
inst.fireTest();
}
public Test2(Test1 inst)
{
this.inst = inst;
}
private Test1 inst;
}
Then teach the inst2 object what inst is.
public class Test1
{
public static void main(String[] args)
{
Test1 inst = new Test1();
Test2 inst2 = new Test2(inst); // <- new code
inst2.doSomething(); // <- new code
}
public void fireTest()
{
System.out.println("fireTest");
}
public Test1()
{
}
}
You only need one main to start the show. Flow of control can still pass through other objects. But at this point I wouldn't call these independent tests. I only used that name to match your code.
What you're looking at is something called reference passing. The fancy term for it is Pure Dependency Injection*. The basic pattern is to build an object graph in main. Once that's built call one method on one object to start the whole thing ticking.
In main you build every object that will live as long as your program does. What you wont find built here are objects that are born later, such as timestamps. A good rule of thumb is to build each of these long lived objects before doing any real work. Since they know about each other they can pass flow of control back and forth between them. There's a lot of power there and if not used well it can get confusing. Look into Architectural Patterns to help keep that simple.
The principle followed here is to separate use from construction. Following that allows you to easily change your mind about what talks to what in one place. It's nice when a design change doesn't force you to rewrite everything.
You have to save your instance somewhere.
If Test1 should be a singleton, you can do:
public class Test1
{
private static Test1 instance;
public static Test1 getInstance()
{
return instance == null ? instance = new Test1() : instance;
}
public static void main(String[] args)
{
Test1 inst = getInstance();
inst.fireTest();
}
public void fireTest()
{
System.out.println("fireTest");
}
}
and in Test2:
public class Test2
{
public static void main(String[] args)
{
Test1.getInstance().fireTest();
}
}
//Edit
As I just learned from #Thomas S. comment, singletons are not a good solution.
See #candied_orange's answer for a better implementation.
I am writing a testing framework using Gauge.
I want some initilization logic performed in one class, and the steps logic to reuse it, like this:
public class A {
protected String property = "";
#BeforeSpec
public void init(){
property = "hello";
}
}
public class B extends A {
#Step("...")
public void verifyProperty() {
assertEquals(property, "hello");
}
}
I can't seem to be able to achieve this. When performing the steps, the "property" is always null.
Placing the #BeforeSpec in class B and calling super.init() works, but I would like to avoid having this call in every test class that extends A.
Has anyone encountered and solved such an issue?
Try to use a static variable:
public class A {
public static String property = "";
#BeforeSpec
public void init(){
property = "hello";
}
}
public class B {
#Step("...")
public void verifyProperty() {
assertEquals(A.property, "hello");
}
}
package java;
//----------------------------- add one more line in here
class Demo {
public static String prt(String name) {
return "my name:" + name;
}
}
public class Sample {
public static void main(String[] args) {
System.out.println(prt("hong"));
}
}
if there is any way to print
my name : hong
,please let me know.
You can do this by creating an instance of the Demo class with new Demo(), like this:
class Demo {
public static String prt(String name) {
return "my name:" + name;
}
}
class Sample {
public static void main(String[] args) {
System.out.println(new Demo().prt("hong"));
}
}
You should be able to reference it via the class name:
Demo.prt("Hong")
however, if you can't use class Demo, then I'm not exactly what real life situation this would be. Methods belong to classes, they do not exist on their own. They must be referenced by classes, or instances of the class. Unless you're providing more context on your question, the answer is No.
However, if you can reference it by object, you could do this:
Demo demo = new Demo();
System.out.println(demo.prt("hong"));
i have coded....
public class mystring{
public String concaT(String s1,String s2){
s1=s1+s2;
return s1;
}
public static void main(String[] args){
String s="stack at";
mystring obj=new mystring();
System.out.println(s.concaT("concat"));
}
}
the thing is, the main method is taking input concaT(s,"concat"), but i want to use s.concaT("concat"). how to make it possible???
s is and object of type String. To do s.concat(String) you have to implements this method into String.class but this isnt possible because String is a final class that cant be extended otherwise you can creat a subclass of String and implement the method there. Why you did not want to use the String.concat(String) which is still implemented in String class?
String class is final defined in library, you can not make sub class it so you can not add any new method to it.
String#concat(java.lang.String) is already available in library use that.
s.concat("concat")
Make it static:
public class mystring{
public static String concaT(String s1,String s2){
s1=s1+s2;
return s1;
}
public static void main(String[] args){
String s="stack at";
mystring obj=new mystring();
System.out.println(concaT(s, "concat"));
}
}
If you want to use it outside the class mystring you must either call it by mystring.concat ("first", "second") or insert an import static mystring.concaT at the beginning of the file to make Java know that you mean that method:
import static mystring.concaT;
// ...
class OtherClass {
public void someMethod () {
String s = concaT ("first", "second");
}
}