Default Initialization To Appropriate Value By Compiler

Feb 20, 2014

I remember reading Instance variables & local variables (in methods) are initialized to appropriate value by the compiler.

Is this true for static class variables and variables in nested classes ?

View Replies


ADVERTISEMENT

Initialization And Default Values

Mar 20, 2015

I have a question related to q44 of Examlab.

public static void main(String... args) {
String i;
// String i = null; that would compile
if (i == null) {// this line does not compile as i was not initialized
System.out.print("A");
} else {
System.out.print("B");
i = "A";
main("A", "B");
}
}

Why does the above code not compile although the statement String i should lead to an initialisation of i to the value of null which is the default value for Strings.

View Replies View Related

Difference Between Explicit In Class Initialization And Non Static Instance Initialization

Dec 20, 2014

I come from a C++ background and I recently started learning Java from "Thinking in Java" 4th Edition by Bruce Eckel.What's the difference between:

// explicit in class initialization
// saw this on page 126

class A {

}

public class B {
A obj1 = new A();
A obj2 = new A();
}

and

// non static instance initialization
// saw this on page 132

class A {
}
public class B {
A obj1;
A obj2;
{
obj1 = new A();
obj2 = new A();
}
}

View Replies View Related

Difference Between Interpreter And Compiler?

Aug 13, 2014

I am reading a java book and find a theory about Interpreter and Compiler. explain different between them?

View Replies View Related

Compiler Error Even After Downcasting

Apr 3, 2014

An example from the SCJP(OCP) book:

Java Code:

class Animal {
void makeNoise() {System.out.println("generic noise"); }
}
class Dog extends Animal {
void makeNoise() {System.out.println("bark"); }
void playDead() { System.out.println("roll over"); }

[Code] .....

The book states that the above code will compile if there is a downcast in the line 14 . But there is a compiler error saying playDead method is not defined for type animal even after downcasting.

View Replies View Related

Java Online Compiler API

Aug 20, 2014

Running a java source code through an online java compiler (in which you will just pass the the source code using a http request, then the compiler will return the output/error of the source code automatically)?.

View Replies View Related

State Machine Compiler

Oct 19, 2014

I am working in software testing, specifically automatic test cases generation. Among the existing forms of test cases, my focus is on the test cases that are composed of sequences of events such as _.event1.event2 eventx()

However, the events can be classified into: sensitive and insensitive. The latter does not affect the system's states, and hence, it can be ignored; while the former affects the states. Anyhow, the sensitive events in the test cases may lead to states explosion and there is a need to prevent that. Therefore, some techniques suggest using one variable to present states and group all similar states together such as using len variable in circular queue. Relatively, the states can be represented by using specific drawings such FSM.

For example, the test cases for circular queue may look like:

add(0).remove().add(1).Front()
add(0).add(1).remove().Front()

which produce the following states:

len=1, rear=0, front=0 and dataQ[0]=0
len=0, rear=0, front=1 and dataQ={0}
len=1, rear=1, front=1 and dataQ[1]=1

len=1, rear=0, front=0 and dataQ[0]=0
len=2, rear=1, front=0 and dataQ[1]=1
len=1, rear=1, front=1 and dataQ={1,0}

As can be seen, every addition/deletion produces a new state. A state is composed of 4 variables: len, rear, front and dataQ. The 1st three variables are integers while the dataQ is an integer array. Nonetheless, the states produced by different test cases can be identical which wastes effort and time. So, there is a need to optimize these states. The search techniques were suggested where the problem can be represented as a search problem and the technique is applied. If we consider Len as a state, then we will have: len=0; 0QSize. However, this does not represent the state but it suits for classifying the states into groups.

In terms of states representation, State Machine/Map Compiler (SMC) was suggested as a modeling mechanism that takes the state machines (i.e. FSM) drawing and generates the code in any preferred language. In SMC, the FSM is represented in a specific syntax (state---transition----next state) and saved in a file (.sm). This file will be compiled by SMC to generate a context class which includes definitions of states, transitions and actions in FSM but still need to be triggered by another class. This class has to call the transitions that modifies the state.

We had created that class and implemented all the methods with their transitions. However, the FSM used was based on 1 variable only (i.e. len). Besides, we are still looking for the SMC results as they will be the input for any search technique to be applied. Supposedly, the states generated by SMC can be used directly in the search technique but this is still questionable.

View Replies View Related

GameLauncher Compiler Error

Apr 24, 2015

I have started to learn JAVA and was referring Head First JAVA book.
I have 3 separate .java files - GuessGame.java , Player.java, GameLauncher.java
I have successfully compiled GuessGame.java & Player.java

But I am getting an error when I am compiling GameLauncher.java.

View Replies View Related

Recursion On Initialization

Apr 29, 2014

I am trying to make a game, for some reason i have begun to get a java.lang.StackOverflowError.

I am not exactly sure how i can fix it. only removing line 14 from infopannel1 (and everything that used that class.) seems to work. im not sure what else i can do to fix it. or why its resulting in stack overflow for that matter.

I am putting in a link for the files i wrote this using bluej (several classes have no relevance, errorv2, demonstration, folderreadertest, ReadWithScanner, saveloadtest, menutest,rannum, and menutestTester. are all irrelivent to my problem.)

[URL] .....

View Replies View Related

Order Of Initialization

Sep 17, 2014

I came across this code which proves that variable initialisation occurs before even constructors are called.However, I am confused over some things.

Firstly, from my understanding, when a new House object is created in this line House h = new House(); , the no-arg constructor of the House class is called.

Since the no-arg constructor House() is called, why are all the creation of Window objects being run first ? Shouldn't Java jump straight into the no-arg constructor House() ?

class Window {
Window(int marker) { System.out.println("Window(" + marker + ")"); }
}
class House {
Window w1 = new Window(1); // Before constructor
House() {
// Show that we’re in the constructor:
System.out.println("House()");
w3 = new Window(33); // Reinitialize w3

[code]...

View Replies View Related

How Compiler Picks What Method To Execute

Feb 20, 2014

public class Main {
private static void foo(Integer a) {
System.out.println("Integer");
}
private static void foo(long a) {
System.out.println("long");

[Code] ....

This code prints long. Why is that? How did compiler decided that it likes long version of foo() method the most. If I had to guess I'd pick int... or possibly Integer as they are most similiar to what was passed. But why long?

View Replies View Related

Why Does Compiler / IDE Force To Use Static In Code

Oct 24, 2014

I'm a beginner fiddling around classes in Java. I noticed on this particular code, Eclipse will give me an error and suggest I put the static keyword in front of the variable.

public class test {
//the following line is where Eclipse puts the static keyword
static FileAccess hello = new FileAccess("D:" + '\', ".mp3");
public static void main(String[] args) {
for (int i = 0; i < hello.getTotalNumberOfFiles(); i++) {

[Code] .....

The FileAccess class is just a class I made while trying to retrieve filenames from my hard drive.

As far as I can tell, it works correctly after I put the static keyword there. I just want to know why it is required in this particular code, considering it didn't need to do that when I made a simpler class while I was getting my feet wet at creating classes in Java.

View Replies View Related

Compiler Does Not Show Any Warning Or Errors

May 18, 2014

public void init(Board board) {
JPanel Panel = new JPanel();
Panel.setLayout(new GridLayout(board.getWidth(), board.getHeight()));
getContentPane().add(Panel, BorderLayout.CENTER);
Panel.setBorder(new LineBorder(Color.BLACK)); // it does not work also
 
[code]....

I have a JFrame. I added two JPanels to the JFrame. The first one is GridLayout and it should be situated at CENTER. Another one is bottomPanel, and that one should be situated at SOUTH.I would like to add two new JPanels(kit and another one) to the bottomPanel.Compiler does not show any warning or errors.The problem is, that bottomPanel situates at NORTH, not at SOUTH. kit situates at CENTER of bottomPanel, not at WEST.

View Replies View Related

Netbeans And Eclipse Compiler Output Is Different

Sep 6, 2014

I was trying to execute the following codes, but the something that I don't undestand was happen. The program was compiled differently according to ouput picture of the program in my java book. Furthermore, then I tried to compile the program in eclipse and NetBeans. I saw that all of output are different each other.

package finallyblock;
public class FinallyBlock {
public static void main(String[] args) {
try{
throwException();
}
catch(Exception exception){
System.err.println("Exception handled in main");

[code]...

View Replies View Related

JSF :: UI Layout Initialization Error

May 12, 2014

I am using netbeans 7.2, glassfish 3.1.2, JSF 2.1 and Primefaces 3.2. When I add more than three menu tabs, I get this error ui layout initialization error the center-pane element does not exist the center-pane is a required element. This is my template.xhtml code:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui">

[Code] ....

View Replies View Related

Error In Array Initialization?

Sep 9, 2014

public class SavingsAccount extends Account {
private static final double MIN_BALANCE = 100.00;
private static final double RATE = 0.035;
public SavingsAccount(Customer customer, double bal, String accountNum,
Transaction[] trans) {
super(customer, bal, accountNum, trans);

[code]....

When I execute this code there is an error in Transaction array initialization. Change the Saving account constructor from (String customer,double balance, String accountnumber,Transaction[] tr) to (String customer,double balance, String accountnumber,Transaction tr)

View Replies View Related

What Are Static Initialization Blocks

Apr 2, 2015

What static initialization blocks do in java?

View Replies View Related

Any Way To Stop Compiler From Calling A Constructor Implicitly?

Nov 13, 2014

Is there any way to compile a program without calling constructor.

View Replies View Related

Lexical Phase Of Compiler - Cannot Make Tokens

Apr 21, 2015

I am trying to make lexical phase of compiler. But when I try to add character (after validating them, whether they are character or not) to token array I cannot do it.

public class Main {
static int counter;
static char ch[];
static char c;
static char token[];
static String read;

[Code] .....

View Replies View Related

JSP :: Accessing ServletConfig Initialization Parameters

May 12, 2014

I am trying a simple program to access ServletConfig initialization parameter in a jsp. But I am not clear how its working. This is web.xml

<web-app>
<servlet>
<servlet-name>Myservlet</servlet-name>
<jsp-file>/JjspInit.jsp</jsp-file>
<init-param>
<param-name>para</param-name>
<param-value>valu</param-value>

[Code] ....

Now If I hit /Allen it sets init parameter for both servlet MyServlet and jsp JjspInit. I don`t have any servlet all I have is a web.xml and a jsp page. But accessing JjspInit.jsp directly gives null.

How hiting the url /Allen prints valu. This code is on JjspInit.jsp .

So does that mean that in web.xml I have registered this jsp as the target for /Allen. And one more question directly accessing the jsp page shows null it means init parameter haven`t been set.

View Replies View Related

Dynamic Initialization Of 2D Array Of Objects?

Aug 15, 2014

if, instead of an ArrayList, can I do the following to initialize a Dyanmic array ? :

First, in my class, I have :

class Example{
private int rows;
private int columns;
private AnotherClass[][] 2DArray;
public Example(int rows, int columns){
this.rows = rows;
this.columns = columns;

[code]....

View Replies View Related

Servlets :: Initialization With Different Scope / Context

Jan 13, 2015

I want to initialize my servlet based on the scope of my application.

For example, I have multiple admins with their respective email address.

I want to perform action within my servlet based on the given environment.

Would this be the right approach, if I set the admin email addresses in my web.xml as init-param:

<servlet>
<servlet-name>ServletName</servlet-name>
<servlet-class>com.mkyong.ServletDemo</servlet-class>
<init-param>
<param-name>admin1</param-name>

[Code] .....

Now if I am in the admin 1 environment, I would initialize my servlet with request parameter admin=1 and the servlet should load email address of admin 1 and similarly when in the environment 2, should load the servlet with admin 2.

call environment 1:
/servlet/Demo?admin=1
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException{

[Code] .....

I could do the same by putting the email address of the respective admin as request param value, but i don't want to the email address to appear in the url.

View Replies View Related

Initialization Of Variables At Class Level

Feb 11, 2014

if I declare

class Example
{
int x=10;
......
}

It is not showing any error but when I declare

class Example
{
int x;
x=10;
............
}

it showing compile time error ....

View Replies View Related

Why Array Initialization Does Not Give Error

Feb 18, 2014

int[][] array1 = {{1, 2, 3}, {}, {1, 2,3, 4, 5}};

it initializes 3 one dimentional array:

1 - {1, 2, 3}
2 - {}
3 - {1, 2,3, 4, 5}

but it only declares two dimensional arrays.

View Replies View Related

Final Field Initialization With Exceptions

May 11, 2014

consider:
 
class A {
     final Object b;
     public A(C c) {
          try {
               b = c.someMethodThatMayThrowSomeException();
          } catch (SomeException e) {
               b = null; // This line results in compiler error: "variable b might already have been assigned"
          }
     } // take away b=null line and you get "variable b might not have been initialized" on this line
}
 
Why? How could 'b' be assigned if the exception was thrown?
 
Btw, the workaround is to wrap the method call:
 
private final Object initB() {
     try {
          return c.someMethodThatMayThrowSomeException();
     } catch (SomeException e) {
          return null;
     }
}
 
and use b = initB(); in the constructor.  But that seems like it should be unnecessary.

View Replies View Related

Will Initialization Of HashMap On Different Place Make Any Difference

Nov 21, 2014

public class HashMapTest {
private static HashMap mp;
public static void main(String[] args) {
// TODO Auto-generated method stub
mp=new HashMap<String, String>();
}
}

This is My Code if I create Object in two place
1)Outside main() method but within class.
2)Inside main() method like above

Will these two approach makes any difference ?

I am asking a general senerio for creating any Type of Objects Primitives or References?

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved