user123475
user123475

Reputation: 1105

Passing object of a class to method of another class

I have a class named employee and it has one private field which is ArrayList named empList. I have another class Database and it has method named insert(). This insert() method takes ArrayList empList as an arguement. How to pass this empList to the insert().

Upvotes: 0

Views: 82

Answers (3)

Endrik
Endrik

Reputation: 2258

You have to construct an object of the type of the first class (which you are not mentioning) inside Database.

class Database{

void insert(ArrayList lst){
.....//your business  logic
}

void invokeInsert(){
YourClass x = new YourClass();
insert(x.getList());
}

class YourClass{
private ArrayList list;

public ArrayList getList(){
return list;
}
}

Upvotes: 1

happy_coding
happy_coding

Reputation: 37

If the insert method is static in the class Database then you could simply pass like this Database.insert(empList); or you can create an object and pass like this. Database db = new Database() db.insert(empList);

Upvotes: 1

anirudh
anirudh

Reputation: 4176

just pass the argument as you would pas any other argument.

insert(empList)

and declare insert as insert(ArrayList<Type> empList)

Upvotes: 1

Related Questions