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 03-21-2008, 05:01 AM
Member
 
Join Date: Nov 2007
Posts: 13
Camden is on a distinguished road
using elements from other classes
I made two classes. Let's say class1 and class2. The class1 has the main method. I am creating a (second) method in class1 and I can take strings, integers etc from my main class e.g.

Code:
public Class1{ public static void main method (String [] args){ int nameOne=0; String nameFirst=null; secondMethod(one, first); //I 'take' both the int and the String. } public void secondMethod (int nameOne, String nameFirst){ //etc }
till here ok, but if I also want to use inside the secondMethod of class1 e.g double name1 that is in class2 what should I do exactly? plz dont give me a theoretic answer, just write what else is needed in the coding of the secondMethod (or generally the Class1). thank you very much.

Last edited by Camden : 03-21-2008 at 05:06 AM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-21-2008, 09:25 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
The Java compiler gets upset when it sees the word "class" and gets busy trying to do something with it. So we generally try to avoid using it. When I tried to compile your Class1 the compiler told me this:
Code:
C:\jexp>javac class1.java class1.java:1: class, interface, or enum expected public Class1 { ^
I renamed your class to avoid this.
Code:
public class FirstClass { public static void main (String[] args) { int nameOne = 57; String nameFirst = "Claude"; // Send these two values off to our two methods. // We are in "static" context (the static modifier is // in the method declaration of this ("main") method so we can // call the firstMethod directly because it also has // the static modifier in its method declaration. firstMethod(nameOne, nameFirst); // The secondMethod method is not static, ie, its method // declaration does not include the static modifier. So // we cannot call it directly. So we use an instance variable // of the enclosing class. // Create an instance of FirstClass and save a reference to it // in a local variable called (most anything!) "firstClass". FirstClass firstClass = new FirstClass(); // instance of firstClass // Use this variable, aka reference, to call the non–static // method secondMethod. firstClass.secondMethod(nameOne, nameFirst); // Trying to call secondMethod directly causes a compile // error: //secondMethod(nameOne, nameFirst); } private static void firstMethod(int n, String s) { System.out.println("In firstMethod: n = " + n + " s = " + s); } private void secondMethod(int n, String s) { System.out.println("In secondMethod: n = " + n + " s = " + s); } }
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
Sorting Elements in a TreeMap Java Tip java.lang 0 04-12-2008 10:47 PM
Elements package BlitzA New To Java 0 12-28-2007 01:58 AM
deleting elements nalinda New To Java 2 12-06-2007 03:42 AM
JSP Scripting Elements JavaForums Java Blogs 0 08-06-2007 05:41 PM
Help with array of elements zoe New To Java 1 07-24-2007 07:33 PM


All times are GMT +3. The time now is 09:30 AM.


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