How to call one class1 from another class2? [duplicate] - java

This question already has answers here:
Call a Class From another class [closed]
(5 answers)
Closed 5 years ago.
Actually am new to Java,How to call one class1 from another class2?
Class1 has main() and other methods.Class2 has different methods.
I want to call class1 from Class2. Please provide the syntax.

You need to first create an object of type class2 and call the methods of it from main method of class1.
class2 c = new class2();
c.methodOfClass2();

Say you have the following classes:
public class A {
int a1 = 15;
public void showMessage() {
System.out.println("Hey!");
}
}
public class B {
}
In case you want your class B to be able to read a1 and call showMessage(), you need to create an object of the class they belong, in the class you'll be working in. Like this:
public class A {
int a1 = 15;
public void showMessage() {
System.out.println("Hey!");
}
}
public class B {
public static void main(String[] args) {
A a = new A();
//call either variables or methods by putting
//a. in front of them
}
}

To call methods of Class1 from Class2
If static method, call by className. e.g - Class1.staticMethodToBeCalledFromClass2();
If non-static method, you need to create object of Class1. e.g - Class1 cls1 = new Class1(); cls1.nonStaticMethodToBeCalledFromClass2();
Assuming Your code :
public class Class1{
public static void main(String[] args) {
}
public void nonStaticMethodTobeCalledFromClass2() {
}
public static void staticMethodTobeCalledFromClass2() {
}
}
public class Class2 {
public void callClass1Here() {
Class1 cls1 = new Class1();
cls1.nonStaticMethodTobeCalledFromClass2();
Class1.staticMethodTobeCalledFromClass2();
}
}
If you look at the code, you will see, to call

Related

How do you call another method from another class with the same name

I want to call a method from another class using this.display();
The problem is, in both classes - there is a method called display()
Without changing the names of the two methods, how do I call upon the one in the other class? What is the proper syntax?
The class I am calling the other one from is class: Formula
When you use this, you are calling the method on the class where you call it.
If you want to call from another method, use a variable.
Example:
class A {
public void display() {
System.out.println ("a");
}
}
class B {
public void display() {
System.out.println ("b");
}
public void example() {
this.display();
A x = new A();
x.display();
}
public static void main(String[] args) {
B x = new B();
x.example();
}
}
The output will be:
b
a
Well this.display() will call the method of the current class. You want to write something like:
OtherClass otherClass = new OtherClass();
otherClass.display();

Invoking method of anonymous class

Java 7
First of all, I'm going to simplify the example to avoid posting unnecesary code. My specific concrete example a little bit complicated, but I' try to preserve the point.
public class Test {
public static void main(String[] args){
Test t = new Test(){ //<---------------------------------------------------------
public void m(){ // |
Test t = new Test(){// |
public void m(){// |
//Here I need to invoke the most inclosing class's m() method
}
//other actions
};
}
public void someMethod(){
//action
}
};
}
public void m(){
}
}
Is it possible to do in Java? I mean, to invoke the method of anonymous class that way?
No it's impossible because there is no reference to the anonymous classes.
This is the only possible way to call the instance m() method :
new Test(){
public void m(){
}
}.m();
By definition according to the oracle documentation here :
Anonymous classes enable you to make your code more concise. They
enable you to declare and instantiate a class at the same time. They
are like local classes except that they do not have a name. Use them
if you need to use a local class only once
So if you have to use one of the methods of your class you have to create a local one.
You cannot access the methods of the anonymous class using normal java, but you are able using reflection:
Test t = new Test{
public void m() {
System.out.println("Welcome to my class");
}
};
Class<?> c = t.getClass();
Method m = c.getDeclaredMethod("m");
// m.setaccessible(true); // if private
m.invoke(t);
Here is a way to do it:
public class Test
{
public static void main(String[] args)
{
Test t = new Test()
{
public void m() // this one will be called
{
Runnable r = new Runnable()
{
#Override
public void run()
{
m();
}
};
Test t = new Test()
{
public void m()
{
r.run();
}
};
}
};
}
public void m()
{
}
}
If the method returns a value, use Callable<V> instead.

how to invoke method of annoymous class from the main method of the outer class

I'm new to Java and is trying to learn the concept of anonymous class. Could someone please tell me how I can invoke the 'awesomeMethod' from the main method of the LocallClassExample?
public class LocalClassExample {
interface Awesome {
public void awesomeMethod();
}
class AwesomeClass {
public int finalInt= 10;
Awesome a1 = new Awesome() {
#Override
public void awesomeMethod() {
System.out.println(finalInt);
}
};
}
public static void main(String[] args) {
}
}
Consider this:
new AwesomeClass().a1.awesomeMethod();
will invoke the method awesomeMethod() on the member variable a1 (which is something Awesome) of the newly created instance of AwesomeClass.
It will get more tricky once your main is outside of your AwesomeClass - and more so once it's outside of the package. In these cases you'd have to provide a getter like
public Awesome getAwesome() {
return a1;
}
Which would when invoked still execute the method as defined in your anonymous class.
Try to use this to create inner class object as:
public static void main(String[] args) {
LocalClassExample.AwesomeClass oi = new LocalClassExample().new AwesomeClass();
oi.awesomeMethod();
}

how to call a overridden method from subclass object in java [duplicate]

This question already has answers here:
Can java call parent overridden method in other objects but not subtype?
(4 answers)
Closed 8 years ago.
Is there a way to call test() in class a from class b object created in class c ?
class a {
void test(){
System.out.println("in a");
}
}
class b extends a {
void test(){
System.out.println("in b");
}
}
public class c{
public static void main(String[] args) {
b refb = new b();
refb.test();
}
}
You can do that only within the test() method of class b like following.
class b extends a {
void test(){
super.test();
System.out.println("in b");
}
}
In Java, all non static private methods are virtual by default. So, there's no way to invoke a#test from b instance unless you modify b#test. The only way to do it (by your current design) is using an instance of a:
public class c{
public static void main(String[] args) {
b refb = new b();
// code to call test() in class a
//this is the only way you have in Java
a refA = new a();
a.test();
}
}

How to pass Object from java class to another java class

I created an instance of a class in java as following:
ABC ab = new ABC();
I want to access this instant ab in another class XYZ. How to make this Object available in class XYZ?
It is difficult to answer your question without more specific information about your problem, but this would certainly work:
You can use a setter to initialize an instance variable in your other class if you want to use it everywhere in that class:
class someClass {
void someMethod() {
ABC ab = new ABC();
XYZ xyz = new XYZ();
xyz.setABC(ab);
}
}
class XYZ {
ABC ab;
//...
void setABC(ABC ab) {
this.ab = ab;
}
//...
void doSomething() {
// do something with instance variable ab
}
}
There are several ways to do what you want to achieve. Some of them might be as follows:
Passing the object reference through a constructor
Here, you would explicitly pass the reference of your reference class when you're creating an object of the actual class.
public class ActualClass{
private ReferenceClass refClassObject;
/**
* Passing through a constructor
*/
public ActualClass(ReferenceClass refClassObject){
this.refClassObject = refClassObject;
}
}
class ReferenceClass{
/**
* Your implementation of the class
*/
}
Using getter/setter methods
In this approach, you would pass the reference of your object through explict public setXX() methods. This approach is more flexible because you can update the reference object as and when you want to (think polymorphism). As an example:
public class ActualClass{
private ReferenceClass refClassObject;
public ActualClass(){
}
public void setReferenceClass(ReferenceClass refClassObject){
this.refClassObject = refClassObject;
}
public ReferenceClass getReferenceClass(){
return refClassObject;
}
}
class ReferenceClass{
/**
* Your implementation of the class
*/
}
Using a combination of constructors and getters/setters
For added flexibility, you might want to initialize your Actual class object with a reference. However if you would also want to keep the option of changing the reference at object at a later stage, go for a combination of both #1 & #2 that I specified above.
public class ActualClass{
private ReferenceClass refClassObject;
public ActualClass(){
}
public ActualClass(ReferenceClass refClassObject){
this.refClassObject = refClassObject;
}
public void setReferenceClass(ReferenceClass refClassObject){
this.refClassObject = refClassObject;
}
public ReferenceClass getReferenceClass(){
return refClassObject;
}
}
class ReferenceClass{
/**
* Your implementation of the class
*/
}
Which one should you choose? Well it would depend on your implementation and requirement.
This answer is exactlty same as Doug Ramsey this link
I tried to explain with the same logic.
public class A {
public void m1() {
System.out.println("inside m1 method");
ABC ab = new ABC(); // 2 object is made and reference is given to ab
XYZ xyz = new XYZ(); 3 object is made and reference is given to xyz
xyz.send(ab); // 4 THIS IS WHAT YOUR QUESTION MEANS
}
}
class XYZ {
ABC ab;
//...
void send(ABC ab) { // 5
this.ab = ab;
System.out.println("inside send");
callme();
}
//...
void callme() { // 6
System.out.println("A is : "+ab.a);
System.out.println("b is : "+ab.b);
// do something with instance variable ab
}
}
public class ABC {
int a = 10;
static int b= 20;
public static void main(String[] args) // called first
{
A a = new A();
a.m1();
}
}
You have two ways to pass object parameter to one class to another.
Passing parameter to a method
public void passMethod(ABC ab) {
}
Passing parameter to a constructor
public class XYZ {
public XYZ(ABC ab) {
}
}
I know this question is old, but If I'm correct you want to transfer an Object into another class to be used.
In order to do that you need a few things
Class XYZ has to have a constructor to take in the parameter "Object" it would something like
class XYZ{
private Object ab
public XYZ(Object ab){
this.ab = ab;//This is the constructor called when you create an XYZ object, and want to use the Object ab in XYZ
}
package demo;
class ABC{
void disp(xyz arg1){
System.out.println("runing disp method in pratics");
System.out.println("x value:"+arg1.x);
arg1.test();
}
}
class xyz{
int x = 67;
void test(){
System.out.println("runing test method in pratics");
}
}
class pratics {
public static void main(String[] args) {
ABC ab=new ABC();
ab.disp(new xyz());
}
}

Categories

Resources