Linked Questions
18 questions linked to/from Use of Initializers vs Constructors in Java
14
votes
3
answers
9k
views
Static Initializers vs Instance Initializers vs Constructors [duplicate]
I am studying for an exam about Java. While I was studying, I have encountered syntaxes in java which are unfamiliar to me. Such as a curly braces({}) unside a class body without a name, some has a ...
1
vote
4
answers
14k
views
static block vs initializer block vs constructor in inheritance [duplicate]
i find this example and i want to understand the logic behind it ? how constructors and static blocks and initializer blocks works in inheritance ? in which stage each one is called ?
public class ...
-4
votes
1
answer
197
views
When to use initializers? [duplicate]
I recently came across the following bit of java syntax:
static {
...
}
apparently this is known as a "static initializer" (see Static Block in Java) and is "executed when the class is loaded".
When ...
-1
votes
1
answer
88
views
In jav8, We are supporting Constructor in abstract class. why we need Constructor in abstract class, if we are having static and non-static blocks [duplicate]
`In jav8, We are supporting Constructor in abstract class. why we need Constructor in abstract class, if we are having static and non-static block support
Code :-
abstract class a{
static
{...
134
votes
10
answers
90k
views
What is an initialization block?
We can put code in a constructor or a method or an initialization block. What is the use of initialization block? Is it necessary that every java program must have it?
2
votes
5
answers
15k
views
Java instance variable declare and Initialize in two statements
Hi I'm having problem with initialization in java, following code give me compile error called : expected instanceInt = 100; but already I have declared it. If these things relate with stack and heap ...
6
votes
6
answers
11k
views
Advantages of Constructor Overloading
I am very new to Java and trying to learn the subject, having previous programming exposure in only HTML/CSS. I have started with Herbert Schildt and progressed through a few pages.
I am unable to ...
2
votes
5
answers
570
views
Can anyone tell me that what is the use of a static block in a class?
public class myclass{
static{
//some statements here
}
//some variables declared here
//some functions defined here
}
4
votes
3
answers
4k
views
JavaFX Application class - difference between default constructor and init method
I have written a little test application that looks like this:
import javafx.application.Application;
import javafx.application.Platform;
import javafx.stage.Stage;
public class Test extends ...
3
votes
2
answers
277
views
initialization block vs constructor vs variable initialization
I'm trying to clean the code of a class that use initialization bloks in a manner I would never do, and I'm just wondering if I am missing some informations. The code looks like this:
@Entity
class ...
0
votes
1
answer
1k
views
Constructor vs Non-Static Init block [duplicate]
I've just learned about init block. I know both(constructor & non-static init block) can be used to initialise members of a class . So, I wanted to know which out of the two is better/more_useful ...
1
vote
1
answer
1k
views
ServerSocketChannel ignores System.setProperty("java.net.preferIPv4Stack") when run from Eclipse-generated jar
When using com.sun.net.httpserver.HttpServer, I observed the following in an application:
When run not from a jar (e.g. from Eclipse or on command line) System.setProperty("java.net.preferIPv4Stack", "...
1
vote
3
answers
96
views
Why do I need to `{...}` to filling an array in the class scope?
What's wrong with the below program?
package test;
public class Test {
byte[] array = new byte[2];
array[0] = 'A';
array[1] = 'B';
}
Look,the IDE indicate some problems(click to enlarge)...
3
votes
2
answers
140
views
Java constructor design
I was reading an open-source code, and there was a constructor designed like this:
public class FeatureSequence2FeatureVector extends Pipe implements Serializable
{
boolean binary;
public ...
-1
votes
1
answer
558
views
Order of field initialization
Consider the following example:
public class Constructor
{
Constructor(int i)
{
System.out.println(i);
}
}
public class Test
{
Constructor c1 = new Constructor(1);
...