Posts Tagged java 7
Java Tutorial 8 – Passing objects as argument
first create 1 new class (i have named it Class3 for make it simple to understand)
and add the code to all thre classes as follows
(after copy and pasting press Alt+Shift+f and it will formate everything…)
Class1:
public class Class1 {
public static void main(String[] args) {
//created two objects
Class3 c3ob = new Class3();
Class2 c2ob = new Class2();
//passing object of Class3 to method of Class2
c2ob.basket(c3ob);
}
}
Class2:
public class Class2 {
int i;
public void basket(Class3 receivedfriut){
for(i=0;i<3;i++)
System.out.println(receivedfriut.fruit[i]);
}
}
Class3:
public class Class3 {
String fruit[] ={ “apple”, “banana”, “grapes” };
}
- we created two objects of class2 and class3
- we passed and object of class3 as arguiment of method of class 2
- we defined basket method in class2 which takes object of Class3 and save it in receivedfruit
- we used receivedfruit object and printed the String array.
This is useful when you are dealing with lots of int,doubles and etc. You can make a different class for it and use it in other Classes and methods by passing objects.
inough bla bla bla, Doubts are invited,
Have a nice day, Peace.
Java Tutorial 7 – passing arguments and returning values
Passing arguments between methods is very easy.
lets use class1 and class2 that we created before.
and change class1 to :
public class class1 {
public static void main(String[] args) {
Class2 c2object= new Class2();
c2object.boy(“Yash”);
c2object.girl(“sweety”);
}
}
and Class2 :
public class Class2 {
public void girl(String name){
System.out.println(name + ” is present”);
}
public void boy(String name){
System.out.println(name + ” is present”);
}
}
Here we have changed
public void boy()
to
public void boy(String name)
so now every time we call boy method it takes a string, see we have passed a string in boy(“Yash”) in class 1.
- boy(“yash”) passes a string to boy function.
- this string is cached into the String name in class 2.
- then we used name to print a line.
You can also pass int, double etc. but when you pass an int you must define other int in definition of method to catch that int.
for example:
public static void main(String[] args){
add(12,13);
public static void add(int x, int y){
System.out.println(x+y);
}
}
we have passed two arguments and cached it in int x and y and used it to display sum.
Returning values:
to return value just add one line at bottom of method called “return (variable)”
in above example we can return the sum like this way:
public static void main(String[] args){
int ans;
ans = add( 12, 13 );
System.out.println(ans);
public static int add(int x, int y){
return (x+y);
}
}
what we have done here is , we simply get 2 int in method and returned the addition of them.
now notice that we have changed public static void add(int x, int y) to public static int add(int x, int y)
it is because now our method returns an int.
same way if your method returns double or String method would be public static double add(int x, int y)
and in class 1 we written ans = add( 12, 13 ) here ans is the variable which will store the returned value.
same way if you returns a String you must have String variable to store it instead of int ans.
In next tutorial we will learn how to pass Objects between methods.