Results 1 to 5 of 5
  1. #1
    johnmerlino is offline Member
    Join Date
    May 2014
    Posts
    52
    Rep Power
    0

    Default cannot be resolved to a type

    I have an instance variable but for some reason I cannot access it in a try with resources block in a public instance method:

    public class Database {
    private Connection conn = null;

    public Database() {
    try {
    connectDatabase();
    ...

    public void connectDatabase() throws SQLException, IOException, JSONException {
    try(conn = getConnection()){

    ...

    Eclipse says conn cannot be resolved to a type:

    Multiple markers at this line
    - conn cannot be resolved to a type
    - Syntax error on token "conn", VariableDeclaratorId expected after this
    token

  2. #2
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    262
    Rep Power
    2

    Default Re: cannot be resolved to a type

    sorry. you were trying a try with resource
    Last edited by superhaNds; 06-04-2014 at 11:50 PM.

  3. #3
    johnmerlino is offline Member
    Join Date
    May 2014
    Posts
    52
    Rep Power
    0

    Default Re: cannot be resolved to a type

    I was using a try with resources of Java 8. But apparently it uses local variables only.

  4. #4
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    262
    Rep Power
    2

    Default Re: cannot be resolved to a type

    Yes, this is the case.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,376
    Rep Power
    5

    Default Re: cannot be resolved to a type

    From the Java 1.8 Language specification, section 14.20.3 try-with-resources.

    "A resource specification declares one or more local variables with initializer expressions to act as resources for the try statement."

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. HttpServlet Cannot be resolved to a type
    By chizbox in forum Eclipse
    Replies: 3
    Last Post: 07-01-2012, 05:09 PM
  2. Cannot be resolved to a type
    By Naxix in forum AWT / Swing
    Replies: 1
    Last Post: 03-18-2012, 12:16 PM
  3. Error: ConsoleProgram cannot be resolved to the type
    By petrohamadi in forum New To Java
    Replies: 5
    Last Post: 06-13-2011, 10:49 PM
  4. Filter cannot be resolved to a type??
    By Harrie_KalaChakra in forum Eclipse
    Replies: 1
    Last Post: 01-15-2010, 09:19 AM
  5. Replies: 1
    Last Post: 12-18-2007, 10:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •