BombSite_A
BombSite_A

Reputation: 310

Already have curly bracket but getting Syntax error, insert "}" to complete class body

So I have this code:

package pages;

import org.openqa.selenium.WebDriver;

public class HomePage extends SiselPage {
    public HomePage(WebDriver driver) {
        super(driver);
        driver.get("http://example.com");
    }

    this.pageTitle = "Site";
}

Which looks perfectly fine to me, but it won't compile. It's giving me two syntax errors, one on line 9 and one on line 12. Line 9 says "Syntax error on token '}', { expected after this token" and line 12 says "Syntax error, insert '}' to complete ClassBody. I've tried rewriting the class from scratch and that didn't work. This, however, compiles:

package pages;

import org.openqa.selenium.WebDriver;

public class HomePage extends SiselPage {
    public HomePage(WebDriver driver) {
        super(driver);
        driver.get("http://example.com");
    }{

    this.pageTitle = "Site";
}}

so do I just use that or what? This seems weird to me. All my other classes are formatted like this and they all work. I'm using eclipse. Thanks!

Upvotes: 0

Views: 1041

Answers (3)

Aneesh Ashutosh
Aneesh Ashutosh

Reputation: 772

Your code, as structured, places this.pageTitle = "Site"; outside the method body and in the class body. Instead, move it within the method body or change it to a field. This solution is documented below:

public class HomePage extends SiselPage
{
    public HomePage(WebDriver driver)
    {
        super(driver);
        driver.get("http://example.com");
        this.pageTitle = "Site";
    }
 }

Upvotes: 1

Andrew_CS
Andrew_CS

Reputation: 2562

this.pageTitle = "Site"; has to be in a constructor or a method.

Upvotes: 0

Jigar Joshi
Jigar Joshi

Reputation: 240898

this.pageTitle = "Site";

this statement needs to be part of executable block (constructor, method, static initializer block)

in your second case you have initializer block which is similar to static initializerr block but without static keyword it is member field initializer

Upvotes: 3

Related Questions