开发者

Question about Java class

I am just learning Java as a hobby.

Ho开发者_如何学Gow do I make a class define a field? E.g.

private string[] biscuitlist

Thank you for any help provided.


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.


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.


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.


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.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜