Saturday, December 11, 2010

when object is created when refrence is assigned???

*Remeber object is always created when you declare an object of class type and refrence is always assigned when u called constructor of that class.
for example

class welcome{
private String greet;
public welcome()
{
greet="Welcome to Java......................!!!!";
}
public void displayGreeting()
{
System.out.println("Greeting :   "+greet);
}
}
public class Demo
{
public static void Main(String[] args)
{
1) welcome w;          
2) w.displayGreeting();           //generates an error of uninitialized object but the matter it has no refrence and                  dont know where to point.
3) welcome wel=new welcome();         //object type :welcome and refrence of:welcome
4) wel.displayGreeting();
}
}
*Remember statment 1 and 2 will not compile and elt the program run but statment 3 and 4 will behave exactly as like an normal and successful program is supposed to behave.

how to call methods of a class???

Lets make again the welcome programme with different strategy so that we  could see how to call a method belonging to a class vis its object.

class welcome{
private String greet;
public welcome()
{
greet="Welcome to Java......................!!!!";
}
public void displayGreeting()
{
System.out.println("Greeting :   "+greet);
}
}
public class Demo
{
public static void Main(String[] args)
{
welcome w=new welcome(); /////now constructor will only initialize and will do nothing else
w.displayGreeting();
}
}
*This programe behave Exactly same as last one but biggest difference is that here constructor only initializes and does nothing else until 
w.displayGreeing();
line called no greetings are displayed on screen.

how can i create a class??

Usually when you create a project a class is generated for you automatically by java IDE ( i m using netbeans 6.9.1). But thats not all about you can make almost as many classes as u want:
for a generic class m making a class with its constructor displaying welcome message
if you want to add new class in same folder as the main class dont use identifier as public(m doing so)
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class welcome{
public welcome()
{
System.out.println("Welcome to Java......................!!!!");
}
}
public class Demo
{
public static void Main(String[] args)
{
welcome w=new welcome();
}
}
*Remember classes are always like a container which contains some things and any thing belonging to class are called via its object.
*Remember
public welcome(){}
its not a method in fact it is constructor. Code in consructor execute before all when we create an object of a class thats y all initialization code are inside of constructor.

what are the classes and whats its importance in java??

Classes are the basic building block of java. In java every thing is based on classes. Not only java classes are very important in almost every object oriented language in the programming world.Classes provide encapsulation to the programms and make the application to the user like a black box( whose internal working is not concerned just what it does is the real matter to concerned about).

What does the break statment do???

break statment is usually used to stop the loop execution immediately with our letting it to complete its total iterations. It is usually used for infinite loops to search for a specific condition.
for example
public class Demo
{
public static void Main(String[] args)
{
int i=0;
while(1){
i++;
if(i= =10)
{
break;                    //will jump outside the loop when i equals 10
}
}
}
}

Sunday, December 5, 2010

General comments

Hey Guys !! If feel some difficulty or wana some knowledge about other topic in java but write here in your comments or inbox me or leave your email id . i will help you be sending you the programme over there.

General comments

Hey Guys !! If feel some difficulty or wana some knowledge about other topic in java but write here in your comments or inbox me or leave your email id . i will help you be sending you the programme over there.