I am relatively new to java, and i really don't understand some of the things going on.
A freind gave me his java exam from his first year at universtity, and said it was relatively easy, i still do not understand it, so i thought i would ask here to see what your answers were and to see if you could explain them so more, as he is not a whole lot of help. I have looked into reasearch about all the topics, tho the internet is confusing me.

Consider the class counter below:
public class Counter {
final static int MAX_VALUE = 100;

static String description = "This class creates counters";

int value;
Counter() {value = 1;}
Counter (int initialValue) {value = initialValue;}

int getCounter() {return value;}

void setCounter(Int newValue) {value = newValue}

void incrementCounter() {++value;}
void decrementCounter() {--value;}

void resetCounter() {value = 0;}

static String getDescription() {return description}

}


Identify all the members and constructors in the class COunter, grouping them as static or instance and variables or methods?

I am relatively new to java, and i really don't understand some of the things going on.
A freind gave me his java exam from his first year at universtity, and said it was relatively easy, i still do not understand it, so i thought i would ask here to see what your answers were and to see if you could explain them so more, as he is not a whole lot of help. I have looked into reasearch about all the topics, tho the internet is confusing me.

Consider the class counter below:
public class Counter {
final static int MAX_VALUE = 100;

static String description = "This class creates counters";

int value;
Counter() {value = 1;}
Counter (int initialValue) {value = initialValue;}

int getCounter() {return value;}

void setCounter(Int newValue) {value = newValue}

void incrementCounter() {++value;}
void decrementCounter() {--value;}

void resetCounter() {value = 0;}

static String getDescription() {return description}

}


Identify all the members and constructors in the class COunter, grouping them as static or instance and variables or methods?

Ok, here you have two constructors, the first one (Counter()) with no parameters, and the other one: Counter(int initValue). You need constructors in creating objects. Here you could create a Counter object by: Counter myCounter = new Counter(); - this would start counting from 1, or Counter myCounter = new Counter(100) - this counts from 100.

You have 2 variables. A variable is something that holds a specific value for you; You can change a variable's value any time you want. A variable has an access modifier (not necesarely) a name, and a type. Your variables are: description - holds characters and value - holds numbers.

You also have a constant here: MAX_VALUE. A constant has the keyword final. A constant's value cannot change throughout the program's run.

Everything else go into the method category. The ones starting with void don't return any value.

Static things are everything having the keyword static. These types of vars, consts, or methods can be accessed without creating an instance of the Counter object. The instance fields and methods can be accessed only if you create such instances (remember? through constructors)

This article has been dead for over six months. Start a new discussion instead.