Search code examples
javaclassmethodssplitvoid

I need to use multiple Strings created in a method inside a class in a different class


So basically i have a method in a class, lets say Class A, that when you give it a String, uses a Split, to create multiple strings. This method would be something like this:

Public void xxxx(String entry){

     String[] Parts=entry.split(" ");
     String part1=Parts[0]; 
     String part2=Parts[1];

So now, i want to use this two strings, inside an if(....) in a different class, how do i do it? I need each parts (there are more than 2) in different if, and sometimes all together so, how do i use them? thanks


Solution

  • It isn't clear from your description how these classes and methods work together. But since all your xxxx method does is split strings it would be best to make it a static utility class.

    Then you can do something like this.

    class MyClass {
         // Note that this method doesn't really add any benefit to
         // simply using String.split().
         public static String[] xxxx(String entry){
            String[] parts=entry.split(" ");
            return parts;
         }
    }
    
    class OtherClass {
        public void someMethod(String str) {
            String[] p = MyClass.xxxx(str);
            System.out.println(p[0]);
            System.out.println(p[1]);
        }
    }
    
    

    There are many other possibilities. It all depends on how you want methods and classes to interact with each other.