Results 1 to 2 of 2
  1. #1
    itsme is offline Member
    Join Date
    Dec 2007
    Posts
    3
    Rep Power
    0

    Default Calling a variable from main to another class

    Hi

    I am trying to access a variable in the main class from another class.This variable is passed as an argument in the main class. I want to check the valuue of the variable passed and do some manipulations in the 2nd class.
    Here the variable I want to access is 'prod_test'

    //MAIN CLASS
    public class TreasuryRpt5 {
    private static String prod_test = null;
    public TreasuryRpt5() {
    super();
    }
    public static void main(String args[]) {

    if (args.length != 1) {
    System.out.println("ProdTest should be passed into TreasuryRpt5.");
    System.exit(1);
    }
    prod_test = args[0];

    //sendmailCLASS ( I want to check the value of prod_test and do some looping)

    public class SendEmail {
    public SendEmail() {
    super();
    }
    public boolean sendMail(
    String toString,
    String subject,
    String body)
    throws Exception {

    try {

    String from = " Web Application";
    String to = toString;

    String mailServer = "localhost"; // or another mail host

  2. #2
    ShoeNinja's Avatar
    ShoeNinja is offline Senior Member
    Join Date
    Oct 2007
    Posts
    124
    Rep Power
    0

    Default

    Since you want to variable to go to a different class, the first thing you will need to do is create an instance of that class in your main method.

    Java Code:
    SendEmail foo = new SendEmail();
    Now that you have your object, you can call any public methods from that class. To get a variable that exists in the main class to your SendEmail class, all you have to do is pass it. Take a look at the signature of the only method you have in the sendEmail class. See all the Strings in ()'s after the method name? Those are variables that are going to be passed to the method. So let's say that you want to send prodTest to a method called testProd().

    - Since you are passing a String, the method signature has to show that the method accepts a String.

    Java Code:
    public **return type here** testProd(String input){}
    Now you just call that method from main.

    Java Code:
    foo.prodTest(testProd);

Similar Threads

  1. Calling a method in another class
    By uncopywritable in forum New To Java
    Replies: 9
    Last Post: 10-22-2012, 05:01 PM
  2. Calling main method
    By eva in forum New To Java
    Replies: 7
    Last Post: 11-06-2009, 02:37 PM
  3. Main startup class
    By j0h@nb in forum New To Java
    Replies: 13
    Last Post: 11-30-2007, 06:44 AM
  4. calling array from main
    By nalinda in forum New To Java
    Replies: 1
    Last Post: 11-17-2007, 10:41 PM
  5. Problem calling another class
    By adlb1300 in forum New To Java
    Replies: 3
    Last Post: 10-25-2007, 03:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •