3 Replies - 324 Views - Last Post: 19 December 2013 - 03:11 PM Rate Topic: -----

#1 AviGreenhut  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 06-December 13

First time compiling a Java program from the command line errors

Posted 19 December 2013 - 03:02 PM

Hi,

These are my errors

Pencil.java:4: error: <identifier> expected
       Public int length; 
            ( arrow pointing up between Public and int)

Pencil.java:4: error: <identifier> expected
       Public int length; 
            ( arrow pointing up at semi colon)

Here is my code exactly as my instructors:

class Pencil {

      private String color = "red";
      public int length;
      public float diameter;
      public static long nextID = 0;

      public void setColor (String myColor) {

                  color = myColor;

      }


       public  String getColor()  {

                return  color;

      }



}





Thank you in advance, Avi

This post has been edited by jon.kiparsky: 19 December 2013 - 03:04 PM
Reason for edit:: [code] tags


Is This A Good Question/Topic? 0
  • +

Replies To: First time compiling a Java program from the command line errors

#2 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7771
  • View blog
  • Posts: 13,137
  • Joined: 19-March 11

Re: First time compiling a Java program from the command line errors

Posted 19 December 2013 - 03:07 PM

"Public" should be "public".

JAva is casE seNsiTive, so the capital letter matters. As you have it here, the compiler thinks you're trying to make an instance of class Public, and it's telling you, okay, you can have a type called Public but you can't call an instance "int" because that's a reserved word.

As a matter of good OO practice, you should avoid making fields public unless they're also final. A public field can be changed by some any object that can see it, which can lead to all sorts of strange bugs. The goal of object-oriented programming (and you'll come to this later) is to encapsulate data, so each object is responsible for its own data.

This post has been edited by jon.kiparsky: 19 December 2013 - 03:10 PM

Was This Post Helpful? 1
  • +
  • -

#3 AviGreenhut  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 06-December 13

Re: First time compiling a Java program from the command line errors

Posted 19 December 2013 - 03:10 PM

Ah sorry John, thank you for putting tags around the code will do next time. :) Avi

Jon not John Thanks

aha, how did I not see that...got it...noobness :)
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7771
  • View blog
  • Posts: 13,137
  • Joined: 19-March 11

Re: First time compiling a Java program from the command line errors

Posted 19 December 2013 - 03:11 PM

Mistakes are good. They teach you a lot, if you choose to learn from them.
The best procedure for learning Java is to make lots of mistakes, and repeat them as few times as possible.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1