Interpreting an '{' expected error


#1



https://www.codecademy.com/en/courses/learn-java/projects/droid?link_content_target=interstitial_project


I receive the following error message when running the code:

Droid.java:1: error: '{' expected public class Droid() {

If I'm interpreting the error correctly, an opening curly bracket '{' is missing in line 1 of my code.

Just to make sure I've checked all the curly brackets in the code but I can't seem to find any one pair that is missing a bracket.

Any help identifying my error will be much appreciated.
Thx!


public class Droid() {
  
  // Instance variables
  int batteryLevel;
  
  // Constructor
  public Droid() {
    batteryLevel = 100; //new objects will have 100% battery
  } // end constructor
  
  // Methods
  public void activate() {
    System.out.println("Droid activated. How can I help you?");
    batteryLevel = batteryLevel - 5;
    System.out.println("Battery level is: " + batteryLevel + "percent.");
  } // end activate method
  
  public void chargeBattery(int hours) {
    System.out.println("Droid charging...");
    batteryLevel = batteryLevel + hours;
    if (batteryLevel < 100) {
      batteryLevel = 100;
      System.out.println("Battery level is: " + batteryLevel + " percent.");
    } // end if
    else {
      System.out.println("Battery level is: " + batteryLevel + " percent.");
    } // end else
  } // end chargeBattery method
  
  public void checkBatteryLevel() {
    System.out.println("Battery level is: " + batteryLevel + "percent.");
  } // end checkBatteryLevel
  
  public void hover(int feet) {
    if (feet > 2) {
      System.out.println("Error! I cannot hover above 2 feet.");
    } // end if
    else {
      System.out.println("Hovering...");
      batteryLevel = batteryLevel - 20;
      System.out.println("Battery level is: " + batteryLevel + "percent.");
    } // end else
  } // end hover method
  
  // Main method
  public static void main(String[] args) {
    Droid Beni = new Droid();
    Beni.activate();
    Beni.chargeBattery(5);
    Beni.hover(1);
    
  } // end main method
} // end public class Droid


#2

Is that the full error message? My java compiler points where it expected {

You can also consider what you were doing at that location and look up the syntax for it

Further, you may want to compile often when you're not totally confident about syntax/error messages, so that when you get an error message you know it's in something that you just added (and also consider how the error message relates to the problem so that when you have more code later you know what it means)


#3

Yes that is indeed the full error.
I guess I'll have to rewrite it on a code editor and compile on every step as you suggest. Maybe I'll have better luck identifying the error.
Thanks for the tip.


#4

Hi avidavi79,

In case you have not rectified the error so far, you have a syntax error on Line 1:

public class ClassName {
     \\ The constructor contains the parameters required.
     public ClassName() {
     }
}

The code runs correctly with that change made.


#5

Yes I did in fact find the error as soon as I started writing in the code editor. Thank you very much for your answer :slight_smile:

While compiling the code I also got another error from the javac which stated that since the Droid class was public, the file should be renamed from droid.java to Droid.java and this raised a few questions:

  1. is that a convention or simply has to do with inner workings of Java?

  2. can a class be made "private" by simply omitting the word public from the class declaration?

  3. what's the difference between a private and a public class?

  4. what happens when we have private and public classes in the same .java

  5. since the javac requested that I named the file after the class name, what happens when I have 2+ classes in the same .java?

Thank you very much for your help


#6

I will try to answer your questions correctly and accurately. If anyone readings this feels they were inaccurate please comment so we can all learn. If you have any more questions after this feel free to ask as well!

... convention or ... inner workings

A bit of both. The convention is that classes should be named with a capital letter, Droid, and that methods are written with a lowercase letter, droidMethod. However, the .java file and the class name need to match. If your file is saved as Droid.java and you start with public class droid you'll receive an error.

private class ... ?

A class can be made private by labeling private. There are three options: public, private, and protected. I have little experience using protected but public and private are common.

difference between public and private?

A public class can be accessed from the outside, a private class cannot. Same with variables, public variables can be accessed from the outside whereas private variables cannot. Consider the following:

public class Droid {
   public int batteryLevel = 100;
   private int answerToLife = 42;
   public Droid() {}
}

> Droid alpha = new Droid()
> alpha.batteryLevel
100
> alpha.answerToLife
No field in Droid has name 'answerToLife'

public and private in the same .java

It works! However, only the public class, who's name matches the .java file, can be accessed. The private class will not be accessible outside of the .java file's code. Private classes can be written inside the same .java file or even within the same public method.

2+ classes in the same .java?

Two public classes in the same .java file should throw you an error; only one will be accessible. If 1 class is public and 1 is private, no error will arise as was stated previously.


#7

Thank you very much for your answer, it does indeed clarify things a lot.

I also went back to the suggested reading material for the Java lessons and found https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.htmlwhich I which I unintendedly skipped since the Read tag on the external resources is not permanent and resets every time so I tend to loose track of what I've read or haven't.


#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.