java by default Constructor [duplicate] - java

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);

Related

Java Non-static method 'getNumberOfOccupants()' cannot be referenced from a static context [duplicate]

This question already has answers here:
What is the reason behind "non-static method cannot be referenced from a static context"? [duplicate]
(13 answers)
Closed 1 year ago.
Here is my code:
interface Dwelling {
int getNumberOfOccupants();
}
public class House extends Building implements Dwelling {
#Override
public int getNumberOfOccupants() {
return occupants;
}
}
public class ApartmentBuilding extends HighRise implements Dwelling{
#Override
public int getNumberOfOccupants() {
totalOccupants = numberOfFloors * occupantsPerFloor;
return totalOccupants;
}
}
public class Village extends Building{
public int getPopulation(){
size = House.getNumberOfOccupants() + ApartmentBuilding.getNumberOfOccupants();
return size;
}
}
When I try to add the getNumberOfOccupants() methods from the House and ApartmentBuilding classes I am getting a Non-static method 'getNumberOfOccupants()' cannot be referenced from a static context error. I am not sure how I can add these methods to produce the getPopulation() method
you have created instance method getNumberOfOccupants() in the class House and same way in the class ApartmentBuilding. calling like these are static method, that's why you are getting error.

How to add object having 4 attributes in array in java [duplicate]

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;
}
}

Why is it thrown java.lang.InstantiationException? [duplicate]

This question already has answers here:
newInstance() with inner classes
(3 answers)
Closed 5 years ago.
Look at following code:
public class Outer {
public static void main(String[] args) throws Exception {
new Outer().greetWorld();
}
private void greetWorld() throws Exception {
System.out.println(Inner.class.newInstance());
}
public class Inner {
public Inner () {}
public String toString(){
return "HelloWorld";
}
}
}
Why is it thrown java.lang.InstantiationException ?
After all, nested class Inner has nully constructor. Can someone explain it?
The [implicit] first argument in an inner's class' constructor is a reference to its enclosing class. When calling it via reflection, you need to explicitly provide it:
private void greetWorld() throws Exception {
System.out.println(Inner.class.getConstructors()[0].newInstance(this));
}
Your class Inner needs to be instantiated, one solution would be to declare it static.
static class Inner {
public Inner () {}
public String toString(){
return "HelloWorld";
}
}

Can we make object of subclass and call parent class method? [duplicate]

This question already has answers here:
When do I use super()?
(11 answers)
Closed 6 years ago.
public class polymorphism {
public void show()
{
System.out.println();
}
public void show(int i)
{
System.out.println("6");
}
public class B extends polymorphism
{
}
/** * #param args the command line arguments */
public static void main(String[] args)
{
// TODO code application logic here B obj=new B(); obj.show();
}
}
To call a method in an object's parent class, you can use the 'super' keyword.
Ex. super.show();

Create instance of a generic class [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Create instance of generic type in Java?
I got these classes
public abstract class Base {
public String Id = "originalId";
}
public class InstanceOfBase extends Base {
public void setString(String test) {
this.Id = test;
}
}
public class UseIt {
public Test<InstanceOfBase> test = new Test<InstanceOfBase>();
public void run() {
InstanceOfBase instanceOfBase = test.createMe();
System.out.println(instanceOfBase.Id);
}
}
public abstract class Test<E extends Base> {
public E createMe() {
// How do I do this?
return new E();
}
}
The code above does not compile because it does not know how to create E. how can I achieve this?
When I invoke the run method, I expect it should print "originalId".
Unfortunately you cannot create classes from generic types in java. But you can do as Justin Rudd suggested in this thread and write:
public E createMe(Class<E> clazz)
{
return clazz.newInstance();
}
So you use the class archetype to create a new instance. Invoking it could be:
InstanceOfBase instanceOfBase = test.createMe(test.getClass)

Categories