java void and non void constructor
In Java, the constructor is not a method. It only has the name of the class and a specific visibility. If it declares that returns something, then it is not a constructor, not even if it declares that returns a void
. Note the difference here:
public class SomeClass {
public SomeClass() {
//constructor
}
public void SomeClass() {
//a method, NOT a constructor
}
}
Also, if a class doesn't define a constructor, then the compiler will automatically add a default constructor for you.
Is it possible to have a constructor with void return type?
A constructor does not have a result / return type.
If you add a result / return type to a "constructor" you turn it into a method whose name is the same as the class name. Then new
won't be able to use it, and any this(...)
or super(...)
call in the method will be a syntax error.
For your example, you won't actually get an error. That is because Java will add a default no-args constructor for A
... because you haven't actually defined any constructors. The new
in your example will actually be using the default constructor ....
If you changed your code to this:
class A {
void A() { System.err.println("hello"); }
public static void main(string arg[]) {
A a = new A();
}
}
and compiled and ran it, you should see that it DOES NOT give you any output. Remove the void
and you will see output.
so here A() work as a method
It >>is<< a method. But it is not "working". As my version of your example shows ... the method is not being called at all.
Difference between a method with void and a constructor
Constructors are methods that belong to a class that are associated with its creation. When you declare an object using Object a = new Object(); This is where constructors are invoked.
You should use constructors to organize any data that you'll need for the rest of the class. For example, if you are making a Time class, the Time constructor might get the current time and set it in a variable to use later.
Other methods are simply that. They are the methods that do some calculations or work for the class. For example, you might have a method that accepts a date and returns the days between the date entered, and the current date.
Java: pass a non void method with parameter as a parameter of another method
At the current form the code won't compile because you can't really access this.
from the static method.
Other than that you can start off with the following (I've removed the irrelevant parts):
class Sample {
public static < T > void setFieldsMap(int fieldTag,
Function< Integer, T > messageGetter,
String fieldKey ) {
messageGetter.apply(fieldTag);
}
}
public class SampleTest {
private static String toString(Integer val) { // an example of method
System.out.println("Converting " + val);
return val.toString();
}
public static void main(String[] args) {
Sample.setFieldsMap(123, SampleTest::toString, "someKey");
}
}
Another point to consider is that T
is not really used, so it can be substituted with ?
class Sample {
public static void setFieldsMap(int fieldTag,
Function< Integer, ? > messageGetter,
String fieldKey ) {
messageGetter.apply(fieldTag);
}
}
Code in constructor with void not execute
It's not strange, it's its normal behaviour. Constructor does not have a return type
public Test() {
System.out.println("constructor");
}
is a constructor, but
public void Test() {
System.out.println("constructor");
}
is a simple method that you can call using t1.Test()
.
This page lists the differences between constructors and methods:
1) First difference between method vs constructor in Java is that name of constructor must be same with name of the Class but there is no such requirement for method in Java. methods can have any arbitrary name in Java.
2) Second difference between method and constructor in Java is that constructor doesn't have any return type but method has return type and return something unless its void.
3) Third difference between constructor and method in Java is that Constructors are chained and they are called in a particular order, there is no such facility for methods.
4) Unlike method, constructor, yet declared in class doesn't considered as member of Class. Constructors are not inherited by child classes but methods are inherited by child classes until they are made private. on which case they are only visible in class on which they are declared. Similarly private constructor means you can not create object of that class from outside, this is one of the technique used to implement Singleton pattern in Java.
5) Another difference between method and constructor in Java is that special keyword this and super is used to call constructor explicitly. no such thing for method, they have there own name which can be used to call them.
Related Topics
Centering a Jlabel on a JPAnel
Jformattedtextfield Is Not Properly Cleared
What Is Web-Inf Used for in a Java Ee Web Application
What Exactly Is Field Injection and How to Avoid It
Nosuchmethoderror in Javax.Persistence.Table.Indexes()[Ljavax/Persistence/Index
Appending Files to a Zip File with Java
How to Calculate "Time Ago" in Java
How to Execute In() SQL Queries with Spring's Jdbctemplate Effectively
What Is an Outofmemoryerror and How to Debug and Fix It
What's the Difference Between Concurrenthashmap and Collections.Synchronizedmap(Map)
Is There a Destructor for Java
Why Is Spring's Applicationcontext.Getbean Considered Bad
Intellij Inspection Gives "Cannot Resolve Symbol" But Still Compiles Code
Java Integer Compareto() - Why Use Comparison VS. Subtraction