I want to create 5 instances of a class, but no more (error message when instantiated 6th time). Also I want to be able to call each objects fields(id in this case) in custom order, so I need reference variables for those objects which I don't because my getInstance() had to be a static method. How can I output the id's of each object for example in reverse order they were created. Hope this makes sense, if not just tell me how you would normally do this kind of stuff.
public class JustFive {
private static int i=0;
private int id;
public JustFive(int n){
this.id=n;
}
public static void main(String[] args) throws Exception {
getInstance();
getInstance();
getInstance();
getInstance();
getInstance();
}
private static JustFive getInstance() throws Exception{
if(i<5) {
i++;
System.out.println(i+" instance created ");
return new JustFive(i*1000);
} else
throw new Exception("Can't create more than 5 instances of this class");
}
private int getId(){
return this.id;
}
}
Create five JustFive instances, place them in a List<JustFive>, then use a Comparator<JustFive> to sort them in descending order by id.
public static void main(String[] args) throws Exception {
List<JustFive> jfs = Arrays.asList(getInstance(), getInstance(), getInstance(), getInstance(), getInstance());
Collections.sort(jfs, new Comparator<JustFive>(){
#Override
public int compare(JustFive o1, JustFive o2) {
return -1 * new Integer(o1.id).compareTo(o2.getId());
}
});
for (JustFive justFive : jfs) {
System.out.println(justFive.getId());
}
}
Outputs
1 instance created
2 instance created
3 instance created
4 instance created
5 instance created
5000
4000
3000
2000
1000
import java.util.List;
public static void main(String[] args) throws Exception {
List<JustFive> elems = new ArrayList<>();
for (int i = 0; i < 5; i++) {
elems.add(getInstance());
}
// print in reverse order
for (int i = elems.size() - 1; i >= 0; i--) {
System.out.println(elems.get(i).getId());
}
}
Related
public class Test {
private static int counter = 1;
int uniqueId;
public Test() {
uniqueId = counter++;
}
public int getUniqueId() {
return uniqueId;
}
}
public class TestSub {
public static void main(String args[]) {
Test a = new Test();
Test b = new Test();
}
}
Assuming that:
the scope of int is large enough for your needs
you will not work with more than one thread at a time
you do not need randomness (for security reasons)
it should do exactly what you're describing.
I have declared a static variable and am changing its value through a non-static method by invoking it in the Initializer block which will be invoked every time an object is instantiated. Why does this not give me run time or compile time error?
public class FinalKeyWord {
final int age;
static int name;
{
ran();
displayName();
}
public FinalKeyWord() {
this.age = 10;
}
public FinalKeyWord(int a){
this.age = a;
}
void ran(){
Random r = new Random();
int rand = r.nextInt(6);
System.out.println(rand);
name = rand;
}
public void displayAge() {
System.out.println("This is final " + age);
}
public void displayName() {
System.out.println("This is static " + name);
}
public static void main(String[] args) {
FinalKeyWord a = new FinalKeyWord();
//a.displayAge();
//a.displayName();
FinalKeyWord a2 = new FinalKeyWord(35);
//a2.displayName();
}
}
Output:
This is static 2 \n
This is is static 3
a variable being static doesn't mean that you can't change its value later, it means that its allocated once in memory for all instances of the class its in, so whenever you create an new object it will point to the same block in memory for this variable unlike normal variables or instance variables where a new block in memory will be reserved for this variable whenever a new object of this class is created.
From Java Documentation/Tutorials,
Instance methods can access class variables and class methods directly.
So this is perfectly legal,
public class FinalKeyWord {
static int a = 5;
void change() {
a= 10;
}
public static void main(String[] args) {
FinalKeyWord obj = new FinalKeyWord();
System.out.println(a);
obj.change();
System.out.println(a);
}
}
And will print,
5
10
Here I am using static keyword to instantiate a variable And I am calling the variable using two different Objects.I want to print the result as 1 and 2 without using the static keyword.Thanks in advance.
public class Test {
static int a = 1;
public void meth() {
System.out.println(a);
a = a + 1;
}
public static void main(String[] args) {
Test a = new Test();
Test b = new Test();
a.meth(); //prints 1
b.meth(); //prints 2
}
}
If you remove the static keyword, you need to share an int variable in your two instances of Test.
For example, using AtomicInteger as a mutable wrapper for int and providing the object when constructing Test:
public class Test {
private final AtomicInteger a;
// + constructor setting a + getter
public void increment() {
a.incrementAndGet();
}
}
public class Main {
public static void main(String[] args) {
AtomicInteger i = new AtomicInteger()
Test a = new Test(i);
Test b = new Test(i);
System.out.println(i.get()); // prints 0
a.increment();
System.out.println(i.get()); // prints 1
b.increment();
System.out.println(i.get()); // prints 2
}
}
Can i use the following code? It's not throwing any error at the Object but at obj.i. Is this a legal way of using an object? Also, how many ways can i create an object other than using the normal syntax obj s = new obj();
public class Test {
static int i;
static Test obj;
obj.i = 10; //am getting a compilation error here "Syntax error on token "i", VariableDeclaratorId expected after this token"
public static void main(String[] args) {
System.out.println(i+" "+ obj);
}
}
You need to place a static block around the obj.i assignment statement for this to work:
public class Test {
static int i;
static Test obj;
static { obj.i = 10; }
public static void main(String[] args) {
System.out.println(i+" "+ obj);
}
}
This is not, you didn't initialize. Furthermore you might not want to use static.
public static void main(String [] args) {
int i = 10;
Test obj = new Test();
obj.setI(i);
System.out.println("my objects I = "+ obj.getI());
}
now in your Test object
public class Test {
private int i;
public void setI(int i) {
this.i = i;
}
public int getI() {
return this.i;
}
}
public static void main(String args[]) throws Exception {
int maxScore = 0;
Thread student = new Thread(client,????);
student.start();
}
I want student thread to change value of maxScore, how do I do it in Java? (Like in C we can pass the address of maxScore)
You need a class object, if you want to modify value in separate thread. For example:
public class Main {
private static class Score {
public int maxScore;
}
public static void main(String args[]) throws Exception {
final Score score = new Score();
score.maxScore = 1;
System.out.println("Initial maxScore: " + score.maxScore);
Thread student = new Thread() {
#Override
public void run() {
score.maxScore++;
}
};
student.start();
student.join(); // waiting for thread to finish
System.out.println("Result maxScore: " + score.maxScore);
}
}
You can't. There is no way you can change the value of a local variable from another thread.
You can, however, use a mutable type that has an int field, and pass it to the new thread. For example:
public class MutableInt {
private int value;
public void setValue(..) {..}
public int getValue() {..};
}
(Apache commons-lang provide a MutableInt class which you can reuse)
Update: for a global variable you can simple use public static fields. Note that if you are willing not only to store some values in them, but also read them and do stuff depending on that, you would need to use synchronized blocks, or AtomicInteger, depending on the usages.
Also, you can use array (of one element):
public class Main {
public static void main(String args[]) throws Exception {
final int[] score = new int[1];
score[0] = 1;
System.out.println("Initial maxScore: " + score[0]);
Thread student = new Thread() {
#Override
public void run() {
score[0]++;
}
};
student.start();
student.join(); // waiting for thread to finish
System.out.println("Result maxScore: " + score[0]);
}
}
adding Synchronized to the methods was a solution for me, thanks