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();
self.setAge(5);
self.something();
}
void setAge(int x)
{
this.age = x;
}
void something() {
System.out.println(this.age);
}
}
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…
vs
A class variable is a variable…
and once they establish that variable == field, they start using variable (as, it is the preferred term)
i do know how to speak database, especially Oracle 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.
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.
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! you should look twice at your code! didn’t you see you already had a constructor there!)
class member variables (“globals”); also fields, right?
constructors
methods
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 ) {
this.name = name;
this.age = age;
}
void setName( String name ) {
this.name = name;
}
void setAge( int age ) {
this.age = age;
}
String getName() {
return this.name;
}
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 :) )" );
}
}
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”)
constructors
methods
I didn’t address the package statement and imports because those items are non-negotiable, the class file structure must be:
package
imports
class definition
Jream answered this correctly, there is no better answer
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) {
this.name = name;
this.age = age;
}
public String getName() {
return this.name;
}
public int getAge() {
return this.age;
}
}