Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello, I am trying to use the same Object between two different methods but I am having trouble in Java. The methods are being called

Hello, I am trying to use the same Object between two different methods but I am having trouble in Java. The methods are being called but it is by different object. How can I pass the same object to retain the same data. Here is my code. When I print out the objects its listed as ObjA@33909752 for one and ObjA@55f96302 for another. I want them both to be the same like (ObjA@33909752 and ObjA@33909752).

Thanks

import java.io.IOException;

public class Main {

public static void main(String[] args) throws IOException {

// TODO Auto-generated method stub

ObjA obj = new ObjA();

passObj2(obj );

passResult(obj);

}

private static ObjA passResult(ObjA o) throws IOException {

// TODO Auto-generated method stub

ObjA obj1 = new ObjA();

obj1.setObjA();

System.out.print(obj1);

obj1=o;

return o;

}

private static void passObj2(ObjA o) throws IOException {

// TODO Auto-generated method stub

System.out.print(o);

o.setObjB();

}

}

- - - - - - - - - - -

import java.io.IOException;

public class ObjA {

public void setObjA() throws IOException{

int A = 1;

}

public void setObjB() throws IOException{

int B = 1;

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Driven Web Sites

Authors: Joline Morrison, Mike Morrison

2nd Edition

? 061906448X, 978-0619064488

More Books

Students also viewed these Databases questions

Question

What are Measures in OLAP Cubes?

Answered: 1 week ago

Question

How do OLAP Databases provide for Drilling Down into data?

Answered: 1 week ago

Question

How are OLAP Cubes different from Production Relational Databases?

Answered: 1 week ago