Заранее спасибо, и просьба не пинать - я в яве новичек

Модератор: Absurd
Код: Выделить всё
public class TestClass {
public static int counter = 0;
publi int something = 0;
public TestClass() {
counter = counter + 1;
something = something + 1;
}
public static int getCounter() {
return counter;
}
public int getSomething() {
return something;
}
public static void main(Sting[] argv) {
for(int i = 0; i < 10; i++) {
TestClass testObject = new new TestClass();
System.out.println("testObject.something = " + testObject.something);
System.out.println("testObject.getSomething() = " + testObject.getSomething());
// Wrong: Compile Warning
System.out.println("testObject.counter = " + testObject.counter);
// Wrong: Compile Warning
System.out.println("testObject.getCounter() = " + testObject.getCounter());
System.out.println();
}
System.out.println();
System.out.println("TestClass.counter = " + TestClass.counter);
System.out.println("TestClass.getCounter() = " + TestClass.getCounter());
// Wrong: Compile Error
// System.out.println("TestClass.something = " + TestClass.something);
// Wrong: Compile Error
//System.out.println("TestClass.getSomething() = " + TestClass.getSomething());
}
}
testObject.something = 1
testObject.getSomething() = 1
testObject.counter = 1
testObject.getCounter() = 1
testObject.something = 1
testObject.getSomething() = 1
testObject.counter = 2
testObject.getCounter() = 2
testObject.something = 1
testObject.getSomething() = 1
testObject.counter = 3
testObject.getCounter() = 3
....
testObject.something = 1
testObject.getSomething() = 1
testObject.counter = 9
testObject.getCounter() = 9
TestClass.counter = 9
TestClass.getCounter() = 9
Код: Выделить всё
public class God {
private static God instance = null;
private God() {}
public static God getInstance() {
if (instance == null) {
instance = new God();
}
return instance;
}
public String getPray(Sting pray) {
return "Everything will be all right!";
}
}
public class Person {
public String pray() {
God god = God.getInstance();
return god.getPray("Ben, I need help!");
}
}
public class World() {
public static void main(String[] argv) {
for(int i = 0; i < 10; i++) {
Person person = new Person();
String reply = person.pray();
System.out.println("God said to person: " + reply);
}
}
}
Код: Выделить всё
public class Test {
private static int x;
}
Код: Выделить всё
class MyClass
{
public:
static int GetCount() { return count; }
private:
static int count;
};