answersLogoWhite

0


Best Answer

Yes you can and they are known as inner methods. You can even delcare an inner class and methods on it. However, if you want your classess accessible from multiple programs (reusability), then you should not use this practice. This type of facility is useful when you know for sure that your class or method is going to be used only once.

User Avatar

Wiki User

12y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

13y ago

This answer uses the term subroutine in place of the language-specific terms function, method, andprocedure.

No, a variable declared in a subroutine is local and can only be accessed from within that subroutine. (A main is a subroutine.) It must be declared outside of all subroutines and thus be global in order to be accessed from more than one subroutine.

However, in scripting languages such as JavaScript, Python, PHP, and Lisp, global variables may be assigned without first being declared.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

Just like any other method. The only restriction here is that, the method you are calling must be static. The methods return type doesnt matter

Ex:

public static void main(String[] args) {

method1();

int x = method2();

}

public static void method1() {

...

}

public static int method2() {

..

}

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

Of course it's possible. You need to declare and initialize another form of type (another class) and then call it.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Is it possible to declare a variable in a method and use it in a main?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Is possible to declare a variable in a method and declare it a main?

The same identifier (variable name) may be used for at most one variable in each scope. Each method has its own scope, in addition to the global scope which is accessible from all others. However, each scope would have a different variable than every other scope despite using the same name for it.


what is the difference between final and static keywords?

static: we can use the keyword static either to method or to a variable. when we declare to a method,(eg: public static void main(String args[]),we can use this method without any object. when we use to a variable,there will be only one instance of that variable irrespective of how many objects that get created of that class. Final: Usage of final to method or to a variable makes them as constant. It's value cannot be changed...


How you can declare local variable?

variable exit within a function and curly braces is local variable int main() { int x; }


Where the variables are stored in the computer?

Variable stored in the memory block inside the RAM. whenever we declare a variable it would take space in main memory and consume it's size from RAM.


How do you call global variable value in main method if main method contains same variable holds another value?

we can call global variables in main method by using scope resolution operator(::) if local and global have same name for example int x=10; main() { int x=20; cout<<x; //x=20 cout<<::x; //x=10 }


Can you write multiple Mains in C-Sharp programming?

No. There can only be one main method, however you can declare new methods, and call them from the main method. Or you can use multi-threading, to simulate having multiple main methods.


Is it possible to call static methods with out creating object?

Yes, we can access all methods which declare with static means then we can access.. ex: class s{ static method() { System.out.println("Welcome"); } } class fun{ public static void main(String args[]) { method(); } }


Why do you write void to the first of main?

Because main is the first method being executed, the Java Virtual Machine cannot expect any return values from this method. Since void is the term used to refer to a variable that does not return any value, we use the key word void for the method signature of the main method


In c plus plus are local declarations visible to the function in a program?

If you declare a variable inside of any fuction (except main) it will not be available to other functions.


Is it possible to define a java static method inside a main method?

Because, the main method is the starting point of the java program and if we need an object of that class even before the main can be invoked, it is not possible. Hence it is declared static so that the JVM Can acess the main method without having to instantiate that particular class


Why java program is not running without the IOException declaration in main method?

Most likely, one of the methods you're calling is throwing an IOException. You either need to declare it to be thrown in the main method or surround the statement with a try-catch block.


Can you run java program without applying main method?

yes we can run java program without using main. we can run program by declaring the variable static..