Results 1 to 2 of 2
- 12-17-2007, 12:50 PM #1
Member
- Join Date
- Dec 2007
- Posts
- 3
- Rep Power
- 0
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
- 12-18-2007, 03:35 PM #2
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.
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().Java Code:SendEmail foo = new SendEmail();
- Since you are passing a String, the method signature has to show that the method accepts a String.
Now you just call that method from main.Java Code:public **return type here** testProd(String input){}
Java Code:foo.prodTest(testProd);
Similar Threads
-
Calling a method in another class
By uncopywritable in forum New To JavaReplies: 9Last Post: 10-22-2012, 04:01 PM -
Calling main method
By eva in forum New To JavaReplies: 7Last Post: 11-06-2009, 01:37 PM -
Main startup class
By j0h@nb in forum New To JavaReplies: 13Last Post: 11-30-2007, 05:44 AM -
calling array from main
By nalinda in forum New To JavaReplies: 1Last Post: 11-17-2007, 09:41 PM -
Problem calling another class
By adlb1300 in forum New To JavaReplies: 3Last Post: 10-25-2007, 02:05 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks