A doubt in Java

I saw many programs containing objects but in all of them object was declared in static method. So, I have a following doubt.

How to use the objects which are not defined in static methods ?
Can you provide me a example in which object is defined outside static method ?

1 Like

First program flow have to reach some non static method…

// almost Java
class Test {

	String[] array;

	public void run() {
		array = new String[10];
	}
	
	public static void main(String[] args) {
		new Test().run();
	}

}

Can you describe better what is unclear?

From where did you studied this ?
Could you tell me book name ?

You wrote the code. But, you have created a new object in main.

Those are just some basics of Java. If you know what static stands for and also know how class and object (see the small ‘o’) instance differs this should be clear to you. If that is not clear, I can try to describe it later if you want :wink:

What about the object which we define outside static method ? how to use them ?
Example:=> In the following code how to use object “obj1”.

Code:=>
class Test {
int a,b;
String name;
Test obj1;
public static void main(String[] args){
System.out.println(“How to use obj1”);}}

It’s shown in my example how to create instance and run run() method what else do you need?

How to use the objects which are not defined in static methods ?

First of all variables declared inside methods are local variables which unlike instance variables need to be initialized prior to their use. Also their scope is contained in the method they are declared in.

Another point to note is

public static void main(String args[])

is a special method as in when you compile and run your code, program execution will start by invocation of this method. If your program does not have method with above signature compile time error will occur.

Can you provide me a example in which object is defined outside static method ?

Now as you say object is defined or lets say declare outside some static method and you want to access it.Note this also means your Object is an instance variable rather than local. Now if your Object is non static then you cannot access it from static method as static Objects are created and initialized when classes are loaded and not when an actual instance of the class is created. So you have to access it from a instance (non-static)method only. If your object is static then you have access it with either static or non static method.

public class HelloWorld {

    static String firsName = "FirstName";
    String lastName = "LastName";

    public void printName(){
        System.out.println(firsName);
        System.out.println(lastName);
    }

    public static void printFullName(){
        System.out.println(firsName);
        //System.out.println(lastName);//cannot do this
    }


}

If your program does not have method with above signature compile time error will occur.

There is an error, of course class does not need to have main() for compilation…