Results 1 to 19 of 19
- 04-15-2011, 06:39 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 30
- Rep Power
- 0
Can i Pass a variable as a parameter from one method to other method
Hi All,
I am having two different methos- WriteFile(String f,String arrr)
- ReadFile(file f1, String s1);
I want to pass a variable from ReadFile(file f1, String s1) to the WriteFile(String f,String arrr).
How can i do this please provide me some guidelines.
Thanks In Advance.
- 04-15-2011, 06:54 AM #2
Member
- Join Date
- Feb 2011
- Posts
- 64
- Rep Power
- 0
like this?
here's an example of a method calling another method and passing it a variable:
Java Code:public void methodOne(){ String varOne = "some string 1"; String varTwo="some string 2" methodTwo(varOne, varTwo); } public void methodTwo(String one , String two){ this.jLabel1.setText(one); this.jLabel2.setText(two); }
- 04-15-2011, 07:25 AM #3
Member
- Join Date
- Mar 2011
- Posts
- 30
- Rep Power
- 0
Thanks jasonwucinski,
Can i use varOne Outside the method to pass to methodTwo(), without calling it in methodOne();
- 04-15-2011, 07:36 AM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,068
- Blog Entries
- 3
- Rep Power
- 13
Not if you define it locally(in the method) if you make it a class variable it would work how you want though.
- 04-15-2011, 07:42 AM #5
Member
- Join Date
- Mar 2011
- Posts
- 30
- Rep Power
- 0
How can i Use that Local variable outside to the method..
- 04-15-2011, 07:42 AM #6
Member
- Join Date
- Feb 2011
- Posts
- 64
- Rep Power
- 0
sure, if its declared as a global variable in your class. like this:
Java Code:public class MyClass { private String varOne = "a"; public void myMethodOne(){ //this method as access to varOne String NewVar = varOne + "b"; myMethodTwo(NewVar); } public void myMethodTwo(String getVar){ //this method as access to varOne which would = "a" // the passed variable, getVar would = "ab" } }
- 04-15-2011, 07:43 AM #7
Member
- Join Date
- Feb 2011
- Posts
- 64
- Rep Power
- 0
Also, if you need to pass variable between classes, you will need to use an interface, but that's a different, more complicated, topic
- 04-15-2011, 07:47 AM #8
Member
- Join Date
- Mar 2011
- Posts
- 30
- Rep Power
- 0
Thanks jasonwucinski, sunde887,
Java Code:public static void WriteFile(String f, String arr) { try{ File f1=new File(f); f1.createNewFile(); FileWriter fstream = new FileWriter(f,true); BufferedWriter out = new BufferedWriter(fstream); out.write(arr); out.write(System.getProperty("line.separator")); System.out.println("Hello Done"); out.close(); }catch(Exception e){ e.printStackTrace(); }
Java Code:public static void ReadFile(File f1, String f2) { Writer output = null; //int i=2; Sheet sheet; try { output = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f2),"UTF8")); sheet = SpreadSheet.createFromFile(f1).getSheet(0); int nColCount = sheet.getColumnCount(); int nRowCount = sheet.getRowCount(); System.out.println("Rows :"+nRowCount); System.out.println("Cols :"+nColCount); MutableCell cell = null; for(int nRowIndex = 0; nRowIndex < nRowCount; nRowIndex++) { int nColIndex = 0; for( ;nColIndex < nColCount; nColIndex++) { cell=sheet.getCellAt(nColIndex, nRowIndex); System.out.print(cell.getValue()+ ""); output.write(cell.getValue()+""); output.write(System.getProperty("line.separator")); //} System.out.println(); } } output.close(); }catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }
Thanks
- 04-15-2011, 08:06 AM #9
Member
- Join Date
- Feb 2011
- Posts
- 64
- Rep Power
- 0
i find it strange you were able to write that code but not be able to understand variable scope. anyway, look how your writeFile method is constructed. it accepts two variables, both strings. one, presumably is the name of a file, im not sure what the other is for. so, you would call WriteFile like this:
Java Code:WriteFile("MyFilePath://MyFileName", "some string")
Java Code:public static void WriteFile(String f, String arr, String AnotherVar)
Java Code:WriteFile("MyFilePath://MyFileName", "some string", "one more string")
Declaring Member Variables (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
- 04-15-2011, 08:09 AM #10
- 04-15-2011, 08:10 AM #11
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,068
- Blog Entries
- 3
- Rep Power
- 13
I'd like to nitpick and say that it isn't technically a global variable, instead it's an instance variable. It's honestly not a huge difference but global variables in java are more like a static variable.
When you want a variable that can be used in multiple methods you want to give the class an instance variable.
- 04-15-2011, 08:14 AM #12
Nitpick the nitpick. Java does not have global variables at all. ;)
- 04-15-2011, 08:15 AM #13
Member
- Join Date
- Mar 2011
- Posts
- 30
- Rep Power
- 0
Hi Juncky,
I am not asking to write a code line by line... I am requesting for any one who can suggest me the best way to achieve my goal not the Code.
- 04-15-2011, 08:15 AM #14
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,068
- Blog Entries
- 3
- Rep Power
- 13
- 04-15-2011, 08:21 AM #15
Member
- Join Date
- Feb 2011
- Posts
- 64
- Rep Power
- 0
sorry, most of my programming experience is in the .Net's. global variables or class fields, what's the difference lol.
- 04-15-2011, 08:24 AM #16
Member
- Join Date
- Mar 2011
- Posts
- 30
- Rep Power
- 0
Hi sunde887,
So I need to use that variables as a class variable then only i can use into the method.
@ jasonwucinski,
Java Code:WriteFile("MyFilePath://MyFileName", "some string")
Thanks...
- 04-15-2011, 09:13 AM #17
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,068
- Blog Entries
- 3
- Rep Power
- 13
@Jason, perhaps it's a small difference, however there is absolutely a difference between a class variable and a global one, a global variable is something that can be used by anything directly. A static variable is close to this but still a bit different.
A class variable appears like this inside of the class. However outside of the class it cannot be accessed directly and requires you to first create an instance of the object to use it "globally"
I do, of course understand what you were trying to convey, I just felt like I needed to point out the difference.Last edited by sunde887; 04-15-2011 at 09:16 AM.
- 04-18-2011, 06:12 AM #18
Member
- Join Date
- Mar 2011
- Posts
- 30
- Rep Power
- 0
Hi All,
I am Still Confuse that how do i use local variable of one method as parameter for another method.
Please Help me...
Tell me What should i use for this..
Thanks in Advance..
- 04-18-2011, 06:39 AM #19
I'm sure you have already been told. You call the method and pass the value as a parameter.
Java Code:class Foo { Bar b = new Bar(); public void someMethod() { int value = 10; methodInSameClass(value); b.methodInOtherClass(value); } private void methodInSameClass(int v) { System.out.println(2 * v); } } class Bar { public methodInOtherClass(int v) { system.out.println(4 * v); } }
Similar Threads
-
Class<T> in method parameter
By Onra in forum New To JavaReplies: 4Last Post: 03-14-2011, 01:12 AM -
Creating array in method parameter
By Dipke in forum New To JavaReplies: 2Last Post: 02-25-2011, 10:18 AM -
pass value inside method A to method B
By masokis in forum New To JavaReplies: 5Last Post: 09-01-2010, 05:06 AM -
Can a method take itself as parameter?
By bukake in forum New To JavaReplies: 10Last Post: 09-06-2008, 10:26 PM -
Input parameter of Main method
By Java Tip in forum Java TipReplies: 1Last Post: 07-12-2008, 07:24 PM
Bookmarks