Answer to Question #37788 in Java | JSP | JSF for esraa

Question #37788
how can i call a class in another class and how to add in it in the other class
1
Expert's answer
2013-12-18T10:07:28-0500
/**
*
* class(test) that call another class(Class1)
*
*/
class Test {

public static void main(String[] args) {
Class1 c1 = new Class1(0);// creating the object of Class1 type
System.out.println(c1.getA());// and now you can call methods of Class1,
// using object of this class(c1)
c1.setA(2);
System.out.println(c1.getA());
}
}

package A;

public class Class1 {
private int a;// class variable
private int b = 0;// class variable(initialized)

/**
* constructor that initialize class variable
*
* @param a
*/
public Class1(int a) {
this.a = a;
}

/**
* returns class variable
*
* @return
*/
public int getA() {
return a;
}

/**
* set class variable
*
* @param a
*/
public void setA(int a) {
this.a = a;
}

/**
* tests inner class
*
* @return
*/
public int testInner() {
return new InnerClass(10).getB();
}

// example of inner class
private class InnerClass {
public InnerClass(int val) {
b = val;// variables of class container is visible for inner class
}

/**
* just return variable of class container
*
* @return
*/
public int getB() {
return b;
}
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS