views:

123

answers:

2

Hi,

I am trying to understand some java code which for some reason appears to execute its return type class (the SQLExecutionInfo class) and nothing else within the method. Perhaps this is simply how the java works (i.e regardless of whether of what is within the method body the type of class being returned is first executed ??)

The method begins thus:

protected Query.SQLExecutionInfo compileSingleQDB(StatementExpression statement, boolean keyNeeded) throws StatementException, XMLDBCException {

//body of method
System.out.println("body of method");
}



/*****THE REFERENCED SQLExecutionInfo class is a subclassed within Query.java***********/

public static class SQLExecutionInfo {
 public SqlExpression sql = null;
 public StatementInfo sInfo = null;
 public Mapper mapper = null;
 public List childrenQueries = null;
 public int[] idPosition = null;
 public int idCount = -1;

 public SQLExecutionInfo() {
 }

 public SQLExecutionInfo(SqlExpression sql, Mapper mapper) {
  System.out.println("POINT ALPHA2:"+ sql);
  this.sql = sql;
  this.mapper = mapper;
 }

For some reason nothing in the compileSingleQDB method (i.e the body) is executed however the class SQLExecutionInfo is called and System.out.println("POINT ALPHA2:"+ sql) is invoked.

Could anyone please explain to me why this is ?

Thanks, Pablo

Please let me provide more info:

This one has me puzzled,

The code execution all begins here:

     System.out.println("POINT SQL:"+jdbcExecInfo.sql); // Returns null at this stage    
try {
   System.out.println("POINT A");
   jdbcExecInfo = compileSingleQDB(((Variable) Qdb.get(0)).getExpression(), false);
   System.out.println("POINT B");
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
System.out.println("POINT SQL:"+jdbcExecInfo.sql); // Somehow now has a value  ????

Somehow it still reaches POINT B and even if I put: Thread.dumpStack(); and new Error().printStackTrace();

within the compileSingleQDB method nothing comes up.

Also even if I rip out the internals of the compileSingleQDB method and replace it with:

protected Query.SQLExecutionInfo compileSingleQDB(StatementExpression statement, boolean keyNeeded) throws XQueryException, XMLDBCException {

    return null;
}

the string jdbcExecInfo.sql still somehow gets a value

+1  A: 

My previous answer didn't apply after the question was edited.

My guess is that this sample is missing some relevant code.

John Weldon
My edit didn't change any of the code, it just got it all doing the pre-formatted code thingie (added a newline after "thus:").
Adam Jaskiewicz
+3  A: 

The code you posted is insufficient to explain the behaviour. Note though that types or classes are not "called". The print statement is in the constructor, so apparently an instance of SQLExecutionInfo is created somewhere (and no, Java does not do that automatically). That this happens while the print statement at the end of compileSingleQDB() is not executed could have a number of causes: an exception being thrown, multithreading, or simply wrong code.

Try reducing the code to pinpoint the problem until you've either found it yourself or you have a version small enough to post entirely so that we can reproduce the behaviour.

Michael Borgwardt
Thrad.dumpStack(); or new Error().printStackTrace(); inside the constructor will give information about where it was called from.
Tom Hawtin - tackline