简体   繁体   中英

Java passing variables from method in one class into different class

I've got this int variable inside the class StringSplit whose value I need to pass to another class called EndStatement to print out; can't really pass it as a parameter though I think. How can I best get the variable to where I need it? Can someone help with a hint? I've read the Java tutorials but don't quite get them. Variables and passing them around seem to be one of my Achilles' heels in Java programming.

EDIT TO ADD: parseCommands can call several different Statement classes eg EndStatement or PrintlnStatement depending on the first element of an Array parsed from a String which serves as a keyword to a HashMap called commandHash. The Statement classes implement the Directive interface which only has a method called execute with the parameter String[] parts . ( EndStatement implements Directive ). Expanded the parseCommands method to show what's going on.

public class StringSplit
{
    public void parseCommands(String fileName)
    {
      //FileReader and BufferedReader to read a file with the code 
      //to execute line by line into an ArrayList myString

      int lineCounter=0; //need to get this variable's value into class EndStatement

      for (String listString: myString)
      {
        lineCounter++;
        String[] parts=listString.trim.split("[\\s]+", 2)//split String into 2 parts 
                                                         //to get commands
        Directive directive= commandHash.get(parts[0])//parts[0] is the hashmap keyword 
     }

public class EndStatement implements Directive

{
    public void execute(String[] parts)
    {
      //need to get lineCounter here--how?
      System.out.print(lineCounter +"lines processed.");
     }


    public static void  main (String[]args)
    StringSplit ss = new StringSplit();
    ss.parseCommands(args[0]);

}

This is my first time answering a question but I think I'm right.

In StringSplit you want to declare linceCounter in a data field.

public class StringSplit
{
    public void parseCommands(String fileName)
    {
      lineCounter=0; //this is the variable I need to pass into a different class
      for (String listString: myString)
      {
        lineCounter++;
        //more code here
      }
     }

     public int getLineCounter()
     {
          return lineCounter;
     }


     private int lineCounter; //this is what I call a data field, you should declare these as private as oppose to public to comply with encapsulation
}

Then in your main method call getLinceCounter , then pass what it returns to EndStatment .

Does this make sense? Did I understand your question right?

public class StringSplit
{
    private int lineCounter=0; 

    public void parseCommands(String fileName)
    {

        for (String listString: myString)
        {
            lineCounter++;
            //more code here
        }
    }

    public int getLineCounter() {
       return lineCounter;
    }
}

public class EndStatement implements Directive
{
    StringSplit ss = new StringSplit();

    public void execute(String[] parts)
    {
        //need to get lineCounter here--how?
        System.out.print(ss.getLineCounter() +"lines processed.");
    }


    public static void  main (String[]args)
    {
        ss.parseCommands(args[0]);
    }
}

I think you mix some terms. There is no such thing as passing variables from one class to another. I assume that what you want to do is simply be able to access (set/get) your variable outside StringSplit class. In order to do that you must declare lineCounter outside parseCommands method as StringSplit's property. Currently lineCounter is local to parseCommands method and as such cannot be visible/accessed outside that method not mentioning to be able to access it from outside a class/object. Do that:

public class StringSplit
{

    public int lineCounter = 0;
    ...

Now you'll be able to access lineCounter from different methods of the same class and from methods outside your class. Making lineCounter public gives others full access to it. As 'Jon' has pointed out it may be dangerous sometimes but for this example case is acceptable. You may see how writing from outside can be prevented using 'Nurlan's' private field with member used to provide read acces only.

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