Question:
static classes in java?
Khadija
2013-03-27 06:07:18 UTC
public class Bicycle {

private int cadence;
private int gear;
private int speed;

private int id;

private static int numberOfBicycles = 0;


public Bicycle(int startCadence,
int startSpeed,
int startGear){
gear = startGear;
cadence = startCadence;
speed = startSpeed;

id = ++numberOfBicycles;
}

public int getID() {
return id;
}

public static int getNumberOfBicycles() {
return numberOfBicycles;
}










my question is: what is the purpose of having a method for returning the ID and the numberOfBicycles? Won't they be the same number? thanks.
Six answers:
Almighty Wizard
2013-03-27 06:15:53 UTC
No, they won't always be the same.



Static variables are are used class-wide and aren't tied to a specific object you have create.



So, in the case of numberOfBicycles, literally the same numberOfBicycles variable is accessible for all bikes you create. Whereas with the id variable, this is tied specifically to each object created and is only accessible to that related object.



Example:

Create BikeA, BikeB, BikeC in this order.

Here is what is returned from your getID/getNumberOfBicycles methods:

BikeA: 1/3

BikeB: 2/3

BikeC: 3/3



So in short, the values are not the same since ID only exists when you create an object and NumberOfBicycles exists at the Bicycle class level, regardless if you have created any bikes yet.
melda
2016-12-14 08:00:06 UTC
you are able to purely have a static class in spite of if this is a nested class (assorted training interior the comparable .java record). to that end you're able to declare public static class YourClass { //blah } in spite of the undeniable fact that many of the time you would be utilising static techniques from a non-static class. This in basic terms ability that once you instantiate an merchandise there's no longer a replica made up of the tactic. Static techniques can in certainty have return values (they frequently do aside from the main approach). the maths class has extremely some static techniques which include random. You get right of entry to it by utilising utilising here command: Math.random(). If the tactic wasn't static, you're able to opt to instantiate the maths class, and use the created merchandise to get right of entry to the tactic: Math mathCopy = new Math(); mathCopy.random();
deonejuan
2013-03-27 07:44:35 UTC
Your question: 'why don't get any output when I compile? The return function is supposed to return a number right? It seems the only way to do that is through the void method System.out.println(..) etc.'



System.out.println( Bike1.getID() + ", out of " + Bike1.getnumberOfBicycles() );



Also, make code readable. class names are CapitalizedHumpBack. variable names are lowerCaseHumpBack
Saman
2013-03-27 07:40:51 UTC
This is not about static class. In your code,you have only one static variable! However,





public class MakeBike {

public static void main(String [] args)

{

Bicycle Bike1 = new Bicycle(5,4,3);

Bicycle Bike2 = new Bicycle(3,8,6);

Bicycle Bike3 = new Bicycle(8,9,45);







System.out.println(Bike1.getID());

System.out.println(Bike2.getID());

System.out.println(Bike3.getID());



//Wrong with method names.

System.out.println( Bike1.getNumberOfBicycles() );

System.out.println( Bike2.getNumberOfBicycles() );

System.out.println( Bike3.getNumberOfBicycles() );



//when you call Bike1.getNumberOfBicycles(),

//it is called Bike.getNumberOfBicycles() because it

//is a static method.

}

}





//Try this
Chris
2013-03-27 06:18:11 UTC
static functions are members of the class, dynamic functions are members of the instances.



Example code:



Bicycle BMX = new Bicycle(1, 2, 1);

Bicycle OldBike = new Bicycle(2, 3, 4);



Now:

BMX.getID() will return 1.

OldBike.getID() will return 2.

Bicycle.getNumberOfBicycles() will return 2.
madamsmall
2013-03-27 06:17:19 UTC
Numofbicycles will have the total number of objects created because it is static therefore shared across all of them.



ID will be the bicycle number of that object precisely. Such as this is bike 3, that is bike 7 etc.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...