Question:
Java experts:Please answer the question?
Rohit C
2008-05-07 19:29:16 UTC
class bus
{
public static void main(String[] args)
{
int i;
i=10;
System.out.println(i);
}
}
In the above code,I want to know that what kind of variable i is:static or some local variable because inside static main(),non-static variables can't be used?And why is it necessary to initialize i to zero or some other value?And why can't we declare i as static inside main()?
Three answers:
joebutnotjoe
2008-05-07 22:07:34 UTC
Inside the main method, 'i' is a local variable stored on the stack. If another method were to call main, a new 'i' would be created, as each run of a method gets its own frame of the stack and can't 'see' each other's 'i's.



In some languages, declaring 'i' static would mean that it's not on the stack frame but outside it somewhere (ie the heap), and changes to it will be seen by each run of the method it's declared in. I don't remember how this applies to Java, however. Usually, though, I think if you need this type of static, there's probably a better way to abstract to an OO concept.



You have to initialize 'i' before printing it, otherwise 'i' will have the number represented by whatever random pattern of memory bits was in that location before your program went "I'll take this memory piece and call it 'i'!". Java is probably protecting you from this bug (which happens alot in languages that don't, especially C++, forget one =0; and all of a sudden a number that should always be 0-50 is 8567366), and also itself from having to add extra code to initialize to zero when you're probably going to make it something else soon anyway.
?
2016-05-28 17:36:31 UTC
The difference between Java and Javascript should not be signified by mere examples of syntactical variations. Java is a programming language, and therefore a program written in Java must be compiled. With Java you can create stand-alone program, applets, and servlets. Javascript is a scripting language, and therefore does not need a compiler, but rather an interpreter. Javascript runs via a browser and is not used to create stand-alone programs. Those explanations are not even the tip of the iceburg, but they do give a quick overview of the major difference. Java would not be used to create a dynamic website alongside HTML, CSS and whatever else you use; that is the purpose of Javascript. However, if you wished to have some animation, game, or business application as part of your website, then a Java applet could do the trick. The only problem that I see with Java applets today, is that Flash and Silverlight run much faster in the browser.
2008-05-07 22:40:53 UTC
main() is a method. Any time we see a word with parens that means either a constructor or a method, but in flow control of a program it means "go somewhere and consider that list of instructions and come back to this point. In your example, i is an instance variable. To make i a member variable, you have to have an Object. Note in our class Bus, I use the default constructor -- it is not specifically spelled out, but the java compiler makes one... public Bus()



class Bus { // class names, by convention, are Cap

int i;

public static void main(String[] args) {

new Bus();

initThei( 99 );

// or

i = 10; // this is a member variable of bus

}

private void initThei( int value ) {

i = value;

}

}

static puts the expression on the heap...

static int, float, double, long, char... any primitive initializes to 0

static boolean initializes to false

static Object initializes to null



static has a value for free.



The confusion might lie with the fact that main() is a special method. The runtime looks for a main() to start the run. Only one main() is allowed. If a folder full of code has more than one main(), the others are ignored only the first one runs. Most of the time a class Object is separate from the main application.



class Bus {

int i;

public Bus() {

i = 10;

}

}



class MainApp {

Bus bus;

static void main(String[] args) {

bus = new Bus();

bus.i = 99; // changes the 10 into a 99

}

}


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...