The ‘Maya’ of static

What if you want to implement main() function in the program. What should be the syntax?
public static void main( string[] args)
is the customary declaration of main() function followed by its definition.

Why to declare main() function static?
The reason is that there is only one main() method in a particular class and it doesn’t require an object to access the main(). main() is available througout the execution of program.In fact, all the fields and methods declared static are available throughout the class and are stored in a heap whereas local variables are stored in stack

let us take an example, it will clear some of my and ur doubts:
class Demo
{
private int count ;
public static void main(String[ ] args)
{
int newValue;
system.out.println(newValue + ” = ” +count );
}
}
Here, the field private to the class (if not static) when used in main() give Compile Time Error. as main() is static.To make it code compilable, alter the code so that
class Demo
{
private static int count ;
public static void main(String[ ] args)
{
int newValue;
system.out.println(newValue + ” = ” +count );
}
}

When ‘Demo’ is executed , it creates ‘count’ variable immediately as it is declared as a static member of class. But ‘newValue’ is created only when main() is called. Variables inside a method are dynamically created when they are called.

Can static field be shared?
Yes static field can be shared. For example
class A
{
static int a;
A()
{
int value;
}
}
if we instantiate in another class the object a1, a2, a3, a4… of class A. All we share the variable ‘a’ with individual ‘value’ variable. The advantage of such a variable is that a lot of space is saved but it also has disadvantage: any object can modify its value.

How to access static members?
Inside class, static fields are accessed by their name only, while outside class the static field are accessed in this way.
classname.fieldname
only if it is public.

In constructor, can we declare static methods or fields?
The answer is obvious no. The constructor is used to instantiate object and we know that the static members r available throughout the class, not when the object is instantiated.

Now let us consider the code:
class xyz
{
public double x()
{ …. }
public static int getcount()
{
x();
return count;
}
}
It will give an compile time error as static method getcount() cannot access non-static method x().
To access the non-static method, we need to instantiate an object in getcount()
{
xyz x1= new xyz();
x1.x();
return count;
}
Recall C! Is there any such restriction related to static storage class??

Till now, we saw that static methods can access members outside the function only if
(i) they r static or
(ii) they r accessed by the object which belongs to that class.

Are there any such restriction on non-static members?
I didn’t find any, non-static methods can access both static and non-static methods.

There are some static blocks in the program. Like static method, static blocks can access only static members. Static blocks are executed first, that too only once. Static block execution is followed by static sub blocks and then by non-static blocks.

class A
{
static //static block
{

}
{ //non-static block

}
}

Why static blocks are created?
I’m not really sure why they r created but they can be created to avoid repetition of initialization. Remeber, non-static members can use static members.

Why non-static blocks are created?
I think they are useful in creating anonymous classes.

Advertisements

updating xml stored in oracle tables

REM **********************************************************************
REM Set Environment parameters
REM **********************************************************************

SET scan off;

set serveroutput on size 100000;

select ‘START TIME: ‘||to_char(sysdate,’mm/dd/yyyy hh24:mi:ss’) from dual;

DECLARE
xml_text1 clob;
xml_text2 clob;
xml_text3 clob;
xml_text4 clob;
xml_column_name VARCHAR2(50) := ‘demo.xml’;
sql_stmt1 VARCHAR2(2000);
sql_stmt2 VARCHAR2(2000);
sql_stmt3 VARCHAR2(2000);
sql_stmt4 VARCHAR2(2000);
BEGIN
xml_text1 :=’ –….. put your xml data here…

‘;

xml_text2 :=’ –….. put your xml data here…

‘;

xml_text3 :=’ –….. put your xml data here…

‘;

xml_text4 :=’ –….. put your xml data here…

‘;

sql_stmt1 := ‘update XML_TABLE set XML = :1, SYS_UPDATE_DATE = SYSDATE where OBJECT_NAME = :2 and SCHEMA = :3’;

sql_stmt2 := ‘update XML_TABLE  set XML = XML||:1, SYS_UPDATE_DATE = SYSDATE where OBJECT_NAME = :2 and SCHEMA = :3’;

sql_stmt3 := ‘update XML_TABLE set XML = XML||:1, SYS_UPDATE_DATE = SYSDATE where OBJECT_NAME = :2 and SCHEMA = :3’;

sql_stmt4 := ‘update XML_TABLE set XML = XML||:1, SYS_UPDATE_DATE = SYSDATE where OBJECT_NAME = :2 and SCHEMA = :3’;

EXECUTE IMMEDIATE sql_stmt1 USING xml_text1, object_name, schema_name;
EXECUTE IMMEDIATE sql_stmt2 USING xml_text2, object_name, schema_name;
EXECUTE IMMEDIATE sql_stmt3 USING xml_text3, object_name, schema_name;
EXECUTE IMMEDIATE sql_stmt4 USING xml_text4, object_name, schema_name;

commit;

END;
/
SET scan on
REM **********************************************************************
REM Get time
REM **********************************************************************
select ‘END TIME: ‘||to_char(sysdate,’mm/dd/yyyy hh24:mi:ss’) from dual;

Writing your first JDBC Application?

Ok, after a long time, here is core technical blog for all of u.
If u r wondering how to access MySQL database using JAVA program. The answer is JDBC application!

Before going to coding, u must understand certain aspects of JDBC programming.

1. You must include Jconnector in the classpath of ur machine so that Java program can interact with ur database schema. Further, you must import packages to use built-in SQL execution functions in Java.

2. You have to register the JDBC driver so that u can open a communication channel to database.

3. Then, you have open the Connection using getConnection() Method.It’s use is demonstrated later in the code.

4. After, the Connection is established, you need to execute a query. For that purpose, it requires using an object of type Statement for building and submitting an SQL
statement to the database.

5. After the execution of query, the result is stored in ResultSet. You will often use the appropriate ResultSet.getXXX() method to retrieve the data from the result set.

6. Atlast, it is always safe to close all the resources instead of relying on JVM’s garbage collection.

Here is the sample code!
//STEP 1. Import packages

import java.io.*;
import java.sql.*;
public class cleaning
{
public static void main(String[] args)
{

Connection con=null;
Statement stmt =null;
ResultSet rs=null;
try
{
//STEP 2: Register JDBC driver
Class.forName(“com.mysql.jdbc.Driver”);

//STEP 3: Open a connection con=DriverManager.getConnection(“jdbc:mysql://localhost:3306/student?user=root&password=it”);

//STEP 4: Execute a query
stmt=con.createStatement();
String sql;
sql = “SELECT RollNo, FirstName, LastName, FirstSem FROM Finalyr”;
rs = stmt.executeQuery(sql);

//STEP 5: Extract data from result set
while(rs.next()){
String rollno= rs.getString(“rollno”);
String firstname = rs.getString(“firstname”);
String lastname = rs.getString(3);
int firstsem = rs.getInt(4);
System.out.println(“Roll no.: ” + rollno +”, “+ firstname+ ” “+lastname+”,Ist Sem:”+firstsem);
}

//STEP 6: Clean−up environment
rs.close();
stmt.close();
conn.close();
}
catch(SQLException se){
//Handle errors for JDBC
se.printStackTrace();
}
catch(Exception e){
//Handle errors for Class.forName
e.printStackTrace();
}finally{
//finally block used to close resources
try
{
if(con!=null)
con.close();
}
catch(SQLException se){
se.printStackTrace();
}
}
}
}

nJoy!