简体   繁体   中英

How to call a class within Java

have two strings, String1 = hello String2 = world, I want to call a class Hello and send to the two strings. The class should return a boolean value and a string. If the boolean is true it should do the followig:

 System.out.println("Hello to you too!");

Can someone help me out with this code?

First, a terminology problem: you cannot "call a class." You can call a method on a class, such as:

someObject.someMethod(string1, string2);

More to the point, you can't return two different values from a method. You could certainly store two different values in the object and return them from different methods, though. Perhaps a class like:

public class Foo {
    protected boolean booleanThing;
    protected String stringThing;

    public void yourMethod(String string1, String string2) {
        // Do processing
        this.booleanThing = true;
        this.stringThing = "Bar";
    }
    public String getString() {
        return this.stringThing;
    }
    public boolean getBoolean() {
        return this.booleanThing;
    }
}

Which would be used as:

someObject.yourMethod(string1, string2);
boolean b = someObject.getBoolean();
String s = someObject.getString();

Having said all that, though, this may not at all be the best way to solve your actual problem. Perhaps you can explain better what you're trying to accomplish. Perhaps throwing an Exception is better than trying to return a boolean, or perhaps there's another solution entirely.

The more detail we have, the better.

You should review your definition of classes but for now I'll assume this is what you meant, comment if this isn't what your looking for:

public class Hello {
private final String first;
private final String second;

public static void main(String[] args) {
    String s1 = "Hello";
    String s2 = "World";
    Hello h = new Hello(s1,s2);
    if(h.isHelloWorld()) {
        System.out.println("Hello to you too!");
    }
}
private Hello(String first, String second) {
    this.first = first;
    this.second = second;
}

private boolean isHelloWorld() {
    return (first.equals("Hello") && second.equals("World"));
    //If that scares you then do this instead: 
    /**
    if(first.equals("Hello") && second.equals("World") {
        return true;
    } else { return false; }
    **/
}
}

When you run this program it will always print "Hello to you too!", if you change s1 or s2 it won't print anything.

public class Hello{

  boolean val = false;
  String str = "";

  public Hello(String a, String b){
   if(a == "hello" && b == "world"){
     this.val = true;
     this.str = "hello to you too";
   }
 }
 public static void main(String args[]){
   String a = "hello";
   String b = "world";
   Hello hello = new Hello(a,b);
   if(hello.val == true)
      System.out.println(hello.str);
 }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM