In cucumber there are 3 ways you can communicate between steps. 2 involve looking up things by a reference in subsequent steps, the 3rd uses global variables.
Following examples are in Ruby, I'll leave it up to others to translate to Java.
1. Implicitly:
Your subsequent steps look up the last record in the database of the thing you are interested in, e.g.
Given 'there is a user' do
create_user
end
When 'the user logs in' do
user = User.last
login_user(user)
end
2. By name:
Given 'there is a user Fred' do
create_user(name; 'Fred')
end
When 'Fred logs in' do
user = User.where(name: 'Fred').first
create_user(user)
end
3. By using a global variable. The variable is global in Cucumber's World object and so can be accessed in other step definitions
Given "I am a user" do
@i = create_user
end
When "I login" do
login_user(@i)
end
There are advantages and disadvantages to each approach, and each approach effects the language of the scenarios.
I rarely use 1 now. I use 2 occasionally particularly when talking about interactions between multiples e.g. stuff involving users Fred and Sue. I use 3 most of all, and in particular use it with singleton concepts like i, my, mine etc. However I have to warn you that using globals safely requires great discipline, and its very easy to create a great deal of mess if you do it badly. So I'd suggest using 2 if you can't meet those criteria.
All best
Andrew