tags:

views:

86

answers:

4

I am just learning Java as a hobby.

How do I make a class define a field? E.g.

private string[] biscuitlist

Thank you for any help provided.

+5  A: 

Java is case sensitive. You need to declare it as String[], not as string[].

package yourpackage;

public class YourClass {

    private String[] biscuitlist;

}

That said, this is actually not "subclassing". So your question title actually contradicts with the question message. To learn more about Java in general, I strongly recommend to go through the Trails Covering the Basics. Good luck.

BalusC
the link you gave seems to be a rather good resource many thanks for your help
Sure, when you want to learn X, start with the documentation/tutorials/guides which are provided with X. In case of Sun Java, that are the Sun tutorials :) Good luck and happy coding!
BalusC
a bit off topic...the Trails Covering the Basics page makes my eyes hurt.Am I the only one...full red, full blue, busy page. JavaDocs formatting is a lot better :)
George Profenza
A: 

The declaration goes inside the class definition as well.

public class SomeClass {

  private String[] biscuitlist;  // declare the variable

  public SomeClass(){}  // empty constructor

  public String[] getList() {
    // the variable is private so provide a getter for external access
    return biscuitList;  
  }
}

If you declare the variable private only methods inside the class can access it. Checkout access controls to understand this if you need to.

Binary Nerd
A: 

You have defined variable already. Just note,

  1. Java is case sensitive.
  2. All classes starts uppercase, e.g. String, Date, List, etc by convention.
fastcodejava
A: 

A class is basically a set of data (fields) and a bunch of operations(methods) on those fields

public class yourClass{
    //define fields here
    private String[] biscuitlist;  
    // java will automagically set biscuitlist to a null reference

    //make a constructor for your class if it will ever be instantiated
    public yourClass(){
    }

    //do stuff here (methods)
}

So basically defining a field is as simple as typing in the access(public, private, protected) giving it a type (String, int, String[], Object) and giving it a name. if not assigned a value after they will default based on the java API (objects get a null reference, ints get 0 etc.)

CheesePls