Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-17-2007, 02:50 PM
Member
 
Join Date: Dec 2007
Posts: 3
itsme is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-18-2007, 05:35 PM
ShoeNinja's Avatar
Senior Member
 
Join Date: Oct 2007
Posts: 123
ShoeNinja is on a distinguished road
Send a message via AIM to ShoeNinja
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.

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.

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

Code:
foo.prodTest(testProd);
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Calling a method in another class uncopywritable New To Java 8 01-14-2008 06:37 PM
Calling main method eva New To Java 1 12-22-2007 02:14 PM
Main startup class j0h@nb New To Java 13 11-30-2007 07:44 AM
calling array from main nalinda New To Java 1 11-17-2007 11:41 PM
Problem calling another class adlb1300 New To Java 3 10-25-2007 04:05 PM


All times are GMT +3. The time now is 03:50 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org