Beginner Java Questions

I have a few questions if anyone could answer.
I placed them in my code in comments below:

public class Test {

	int age;

	// 0. Is this the constructer?
	// 1. Or would the constructer be called Test (the class name)?
	// 2. What does void mean? It's not in my book!
	public static void main(String[] args) {

		// 3. Do I need to create (this) object inside of itself
		// to call methods like below?
		// (Assuming this isn't a HAS-A from a super class.)
		Test self = new Test();

	void setAge(int x)
		this.age = x;
	void something() {



As I read it, field is the general term, instance or class variable when specificity is needed, although, colloquially among Java programmers, variable is the general term, specificity is determined by context.

Also, yup, you caught me, I was saying member variable when I meant class variable :o

One last thing, when defining a word or term, it’s considered to be extremely bad form to use the word as the definition.

A class variable is a field…


A class variable is a variable…

and once they establish that variable == field, they start using variable (as, it is the preferred term)

I guess we’ll just have to agree that they’re interchangeable.

Though, I will throw my trump card anyway: JLS 4.12.3

i do know how to speak database, especially Oracle :slight_smile: now i’m trying to java. but i must say that in another book i get my hands on, from 2001, when Oracle was not yet all over Sun’s Java, namely “Java Programming for the Absolute Beginner - Course Technology PTR 2001”, they refer to the object’s variables as fields.

also, they define member variables as instance (no static modifier) or class (with the static modifier) variables and refer to them as being fields.

Interesting, but not unexpected. Oracle is, obviously, a database company - in the database world, variables are known as fields.

The question is, are you learning to speak Java, or database?

ok :slight_smile: thanks for response(s).

Thanks for replies.

So the public static main() is so that Java can run the object from a static point, ie: for the master class.

PS you are right on the age :stuck_out_tongue:

Awesome this is making more sense

thanks for the reminder. i knew about member and instance variables, residing inside the outer most curly brackets. i just read about them being called as fields.

The Java Tutorials on Oracle site:

Declaring Member Variables
There are several kinds of variables:
Member variables in a class—these are called fields.
Variables in a method or block of code—these are called local variables.
Variables in method declarations—these are called parameters.

re: fields - In Java, we call them ‘variables’.

I should have said “member and instance variables”, my bad.

Member variables are declared with the static keyword, there is one instance of that variable available to all instances of that class.

Instance variables are more traditional in that each instance of the class gets its own instance of the variable.

So, if we write this:

public class InstanceVsMemberVariables {
  public static void main( String[] args ) {
    SomeClass a = new SomeClass( "A" );
    SomeClass b = new SomeClass( "B" );
    System.out.println( a );
    System.out.println( b );

class SomeClass {
  static String memberString = "MeMbEr";
  String instanceString = "InStAnCe";
  public SomeClass( String name ) {
    memberString += " : " + name;
    instanceString += " : " + name;
  public String toString() {
    return "Instance String : " + instanceString + "\
" +
           "Member String : " + memberString;

We get

Instance String : InStAnCe : A
Member String : MeMbEr : A : B
Instance String : InStAnCe : B
Member String : MeMbEr : A : B

i believe JREAM would benefit more from an answer to “what is a constructor” rather than “which is a constructor”.

0: No.
1: Yes.
2: It means that nothing returns from the method.
3: It depends. By now you’ve probably run into ‘object cannot be referenced from a non-static context’ and tried to fix it by making everything static, all the while thinking ‘this can’t be right’, you’re right, it’s not.

Keep in mind: main is a special method that is used by Java to load and execute your class, until you create an instance of that class within main anything that is not static does not exist.

indeed, a silly question answered the best possible way ! this “little” thing of “only when no constructor” eluded me! thanks JREAM! (shame on you noonnope! :blush: you should look twice at your code! didn’t you see you already had a constructor there!)

class member variables (“globals”); also fields, right?

My book says that “A default constructor will only be created if you don’t create one. That constructor will have no arguments.”

It looks like you are trying to “Overload a Constructor”. In my book the example it shows has the no-argument constructor after the argument one:

Person( String name, int age ) {}
Person() {}

That is all I know right now, I hope someone can give you a better answer :stuck_out_tongue:

i’m also a java newbie, so i’d like to take the Person class a bit further.

i have a question: when you don’t provide a constructor for your class, the compiler it’s supposed to attach an empty one, right? why do i have to write a Person() constructor myself then (without args) in the Person class below, if i want to make a Person() call?

in my Person class i only have the constructor with arguments. in the PersonTest, if i make a call like

Person noonnope = new Person();

the compiler looks for the non-arguments constructor and throws an error.

then, if i write the constructor without arguments (that should have been added by the compiler?) for the Person class, it’s ok.

here are the two classes. for readability i’ve kept out modifiers in Person class.

* Person class exibits methods
* for setting and getting
* name and age
class Person {
    String name;
    int age;

	//implicit constructor for the Person class
	Person() {
		//things to be done
		//i wonder why i have to declare this
		// if the compiler is supposed to 
		//build an empty constructor anyway?
	//explicit constructor for the Person class
	Person( String name, int age ) { = name;
		this.age = age;
    void setName( String name ) { = name;

    void setAge( int age ) {
        this.age = age;
    String getName() {

    int getAge() {
        return this.age;
* PersonTest class
* for testing various calls
public class PersonTest {
    public static void main(String[] args) {
		//testing the explicit constructor for Pearson class
		System.out.println( "Testing the Person class using the explicit construtor call." );
		//create an instance, a new Person object
		//for jream
		Person jream = new Person( "hooknc", 23 );
		//printing its fields
		System.out.println( "JREAM - Name: " + jream.getName() + "  Age: " + jream.getAge() );
		//creating hooknc's instance
		Person hooknc = new Person( "hooknc", 25 );
		//printing fields for hooknc
		System.out.println( "hooknc - Name: " + hooknc.getName() + "  Age: " + hooknc.getAge() );
		//now let's correct data
		System.out.println( "Ooops! Let me correct that... " );
		//setting fileds for jream object
		//using methods rather than a constructor call
		jream.setName( "JREAM" );
		jream.setAge( 25 );
		//printing corrected values
		System.out.println( "JREAM - Name: " + jream.getName() + "  Age: " + jream.getAge() );
		System.out.println( "hooknc - Name: " + hooknc.getName() + "  Age: " + hooknc.getAge() );
		//testing the implicit constructor for Pearson class
		//the one added by the compiler
		System.out.println( "Testing the Person class using the implicit construtor call." );
		//create a new noonnope object
		//see that no arguments are provided this time
		Person noonnope = new Person();
		//using the Person class methods
		//to provide values for its fields
		noonnope.setName( "noonnope" );
		noonnope.setAge( 25 );
		//printing fields values for noonnope
		System.out.println( "noonnope - Name: " + noonnope.getName() + "  Age: " + noonnope.getAge() + " (i'm older than that, but not that much older :) )" );

main(String args) is a special method to make your class “runable”, when you “call” it by its name, it will “take off” executing the code in main().

it has modifiers:

  • public: can be accessed from other classes
  • static: it’s the same for all instances
  • void: doesn’t return anything

and can be parametrized, using arguments in a String array.

One of the beautiful things about Java is that the order of things within your class is irrelevant. That said, there are standard ways of organizing the bits and pieces that make up a class, using a standard form makes it easier to go back and read a piece of code that you or someone else wrote.

My preferred structure is:
class member variables (“globals”)

I didn’t address the package statement and imports because those items are non-negotiable, the class file structure must be:
class definition

Jream answered this correctly, there is no better answer :wink:

am i not reading it well? :slight_smile:

A class variable is a field

An instance variable is a field

they do refer to member variables as fields, class or instance.

the way i read it: fields = member variables = class variables which, when not using static modifier = instance variable.

Here is a different example:

public class Test {
    public static void main(String[] args) {
      // Don't really know your age, just taking a guess.
      Person jream = new Person("JREAM", 25);
      // Not my real age.
      Person hooknc = new Person("hooknc", 25);

      System.out.println("Name: " + jream.getName() + "  Age: " + jream.getAge());
      System.out.println("Name: " + hooknc.getName() + "  Age: " + hooknc.getAge());

public class Person {
    private String name;
    private int age;

    public Person(String name, int age) { = name;
        this.age = age;

    public String getName() {

    public int getAge() {
        return this.age;