1

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.

asked Feb 6, 2010 at 22:49
1

4 Answers 4

5

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.

answered Feb 6, 2010 at 22:51
Sign up to request clarification or add additional context in comments.

3 Comments

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!
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 :)
0

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.

answered Feb 6, 2010 at 23:01

Comments

0

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.
answered Feb 6, 2010 at 23:01

Comments

0

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

answered Feb 6, 2010 at 23:06

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.