This question already has answers here:
How to create a Java class with data fields
(2 answers)
Closed 2 years ago.
Object[] Flights = new Object[10];
This is an Object array, now what i want is to add 4 attributes to each object which includes String and integer?
How can I do this ?
You need to create a custom class and define the attribute that you want
public class Solution {
static class CustomObj {
int id;
String name;
// your properties
}
public static void main(String[] args) {
CustomObj[] objsC = new CustomObj[10];
// your logic
}
}
I believe you can't do this in Java, you absolutely need to create a class.
public class Myclass {
public String myString;
public int myInteger;
}
and create an instance of that class :
public mainClass {
public void main(String args[]){
MyClass myInstance = new MyClass();
myInstance.myString = "the value you want";
myInstance.myInteger = 1;
}
}
Related
This question already has answers here:
Java - get the current class name?
(12 answers)
Closed 3 months ago.
Lets take this for Example:
public class Vehicle {
public enum Car {
CAR1,
CAR2,
CAR3,
CAR4,
}
public enum BIKE {
BIKE1,
BIKE2,
BIKE3
}
}
public class Main {
public static void main(String args[]) {
Vehicle.Car value1 = Vehicle.Car.CAR1;
Vehicle.Bike value2 = Vehicle.Bike.BIKE1;
print(evaluateType(value1));
// Expected Output: Car
print(evaluateType(value2));
// Expected Output: Bike
}
}
Now the use case here is, We have to write the function evaluateType. I was wondering if there is a way to know the type of Enum we are using, if it is of type Bike or Car.
Given the fact the enums are stored as Int in memory, this doesn't seems like it can be done. But looking forward to any suggestion on how this type of situations could be handled.
Thank you.
Not sure if that is what you want but:
public class BaseClass {
public enum PropertyTypeString {
STRING1,
STRING2,
STRING3,
STRING4,
STRING5,
}
public enum PropertyTypeInt {
INT1,
INT2,
INT3
}
}
public class Main {
public static void main(String args[]) {
BaseClass.PropertyTypeString value = BaseClass.PropertyTypeString.STRING1;
BaseClass.PropertyTypeInt value_2 = BaseClass.PropertyTypeInt.INT1;
evaluateType(value);
evaluateType(value_2);
}
public static void evaluateType(Enum value)
{
System.out.println(value.getClass());
}
}
You can attach values to enum, you don't need to stick with basic enum :
public enum Element {
H("Hydrogen"),
HE("Helium"),
// ...
NE("Neon");
public final String label;
private Element(String label) {
this.label = label;
}
}
source : https://www.baeldung.com/java-enum-values
This question already has answers here:
What is the scope of variables declared inside a static block in java?
(4 answers)
Closed 3 years ago.
I was working on some code in which I need to access the variable "hs" present in the static block of one class from another.
Note: Both the class are preset in different packages.
Code is as follow:
public class A{
static {
HashSet<String> hs = new HashSet<>();
}
}
I Googled about it but nothing found anything helpful.
Your help would be very appreciable.
EDIT: I am not allowed to make changes in this file still need to access it from the other file.
Why I need to do this cause I am doing unit testing by JUnit and there is nothing what this block is returning which I can put assertEquals() on. So the option I left with is to test the side-effects and this variable "hs" value is getting changed as a side-effect. That's why I need to access it from another file.
Declare it as public static inside the class and initialize it in static block
class A1{
public static HashSet<String> hs;
static {
hs= new HashSet<>();
}
}
Need create getter and setter for variable "hs".
Class 1:
public class Test {
public static HashSet<String> hs;
static {
hs = new HashSet<>();
hs.add("Test14");
hs.add("Test15");
hs.add("Test16");
}
public static HashSet<String> getHs() {
return hs;
}
public static void setHs(HashSet<String> hs) {
Test.hs = hs;
}
}
Class 2
If you need to use "hs" variable in without static method then:
public class Test2 {
public void test() {
Test ts = new Test();
ts.getHs();
}
}
If you need to use "hs" variable in with static method then:
public class Test2 {
public static void test() {
Test.getHs();
}
}
This question already has answers here:
Why default constructor is required in a parent class if it has an argument-ed constructor?
(12 answers)
Closed 4 years ago.
public class ConStru
{
public ConStru(int bc)
{
}
}
public class MainClass {
public static void main(String[] args)
{
ConStru conStru = new ConStru();
}
}
it is not working. giving the error.
You should pass an integer to the constructor as
ConStru conStru = new ConStru(1);
Also remove public from "ConStru" class,
Add another constructor:
public ConStru()
{
}
Or create object with an int parameter:
ConStru conStru = new ConStru(1234);
This question already has answers here:
How to inherit static field and change it's value?
(4 answers)
Closed 7 years ago.
Here is the sample program:
public class Base {
public static final String FOO = "foo";
public static void main(String[] args) {
Base b = new Base();
Sub s = new Sub();
System.out.print(Base.FOO);
System.out.print(Sub.FOO);
System.out.print(b.FOO);
System.out.print(s.FOO);
System.out.print(((Base)s).FOO);
}
}
class Sub extends Base {
public static final String FOO="bar";
}
When I execute this it's printing foobarfoobarfoo.
Since String FOO is declared as public static final, my understanding is its value cannot be changed anymore. But in the subclass Sub, the value is being changed to bar.
Shouldn't the program throw a compilation error?
Why is it printing foobarfoobarfoo?
Static variables are not inherited, they belong to the class, that's why they are static. The subclasses can have static fields with the same, though.
This question already has answers here:
How to create objects from a class with private constructor?
(6 answers)
Closed 7 years ago.
I want to create object of Base class in another class but Base class constructor is defined as private
Here is my Code
public class Main
{
public static void main(String ... args)
{
//Base objBase = new Base();
//objBase.show();
}
}
class Base
{
private Base()
{
}
public void show()
{
System.out.println("Base Class Show() Method");
}
}
Code inside Base is still allowed to call the constructor, which means it's possible to create objects in a static method:
class Base
{
private Base()
{
}
public void show()
{
System.out.println("Base Class Show() Method");
}
public static Base createBase() {
return new Base();
}
}
and then call the method to create an object:
Base objBase = Base.createBase();
objBase.show();
You cannot create objects of classes if they have private constructors. Objects can be constructed, but only internally. That is how it is.
There are some common cases where a private constructor can be useful:
Singletons
Classes containing only static methods
Classes containing only constants
Type safe enumerations
Hoping this helps.