user2080245
user2080245

Reputation: 13

Eclipse, cannot be resolved

This is from a textbook about App-Development.

import java.awt.TextField;

public class ESA
{
    public void init()
    {
        TextField abc = new TextField();
    }
    public void doSomething()
    {
        abc.setText("Hello World");
    }
}

The Problem is: There is one Error in the code, an there should be a way to solve it with Eclipse. No solution is given the textbook.

In my opinion, the problem is "abc.setText" Eclipse has 6 quick fixes, but none of them work.

Has anyone an idea how to solve it?

Upvotes: 1

Views: 2311

Answers (3)

MirEgal74
MirEgal74

Reputation: 1

I come across this question because I worked on the same problem, perhaps the same textbook.

So that's the reason why I want to answer this question even when it is already two years old.

"... there should be a way to solve it with Eclipse. No solution is given the textbook."

The easy way that is ment here is the "Refactoring". Right click on "abc" and then choose "Refactor" and in the submenu "Convert Local Variable to Field".

All needed changes in the code will be done by Eclipse on its own. The resulting code is the same that is here already posted in the other solutions.

Upvotes: 0

Vaishak Suresh
Vaishak Suresh

Reputation: 5845

import java.awt.TextField;

public class ESA
{
    private TextField abc;
    public void init()
    {
       abc = new TextField();
    }
    public void doSomething()
    {
        abc.setText("Hello World");
    }
}

This should work. abc is not accessible in the doSomething() of your code.

Upvotes: 1

Reimeus
Reimeus

Reputation: 159754

The TextField variable abc is not available in the scope of the method doSomething as it is defined locally in init. It can either be declared as a class member variable or passed into the method. You could add

private TextField abc;

and replace

TextField abc = new TextField();

with

abc = new TextField();

Understanding Instance and Class Members

Upvotes: 4

Related Questions