Declaring variables
Example: String example;
Declaring Multiple Variables
Example: String ex1, ex2;
Declaring class variables
(Hints-Must include static in the declaration before the variable type;
must place declaration within the body of the class but not within any of the class methods)
Example of declaring a class variable
class HelloApp
{
static String hellomessage;
public static void main(String[] args)
{
hellomessage = "hi";
System.out.println(hellomessage);
}
}
and this is the output
run:
hi
BUILD SUCCESSFUL (total time: 0 seconds)
---------------------------------------------------------------------------------------------
I'm using the Netbeans IDE 7.3.1 from here on with my tutorials unless otherwise specified by JFD.
I keep having the same problem with the example in the JFD. It specifies each example with "public class HelloApp" It won't build properly unless I take away the "public"
Not sure what public does exactly..
The JFD says that public indicates that a method being declared here should have public access.
Meaning that classes other than the HelloApp class can use it.
Placing the variable at end of class
class HelloApp
{
static String hellomessage;
public static void main(String[] args)
{
hellomessage = "hi";
System.out.println(hellomessage);
}
}
run:
hi
BUILD SUCCESSFUL (total time: 0 seconds)
----------------------------------------------------------------------------------------------------------
Declaring Local Variables
(HINT: is picky about where one places the declaration - Before the first statement that actually uses it)
Example:
class HelloApp
{
public static void main(String[] args)
{
String helloMessage;
helloMessage = "hey";
System.out.println(helloMessage);
}
}
run:
hey
BUILD SUCCESSFUL (total time: 0 seconds)
------------------------------------------------------------------------------------------
The next to last part of JFD in the variable section is Initializing variables.
class testApp
{
public static void main(String[] args)
{
int i;
i = 0;
System.out.println("i is " + i);
}
}
in the example given the variable is initialized to a value of zero and this happens before the println outputs the variable's value.
this was the output of the example in the book.
run:
i is 0
BUILD SUCCESSFUL (total time: 4 seconds)
Last example of a initialized variable... initializing variables with initializers... (initially)
The book states that this is so we can combine a declaration and an assignment statement into one statement.
class testApp
{
public static void main(String[] args)
{
int x = 2;
int y = 1;
String lastName = "Lowe";
double radius = 15.4;
System.out.println(x + y);
System.out.println(lastName);
System.out.println(radius);
}
}
run:
3
Lowe
15.4
BUILD SUCCESSFUL (total time: 2 seconds)
Now this example has me confused a little.
It was this initially
int x = 2;
String lastName = "Lowe";
double radius = 15.4;
this is a combination of a declaration and an assignment statement...
I tried to compile this to state what each variable is in the println method but, only got an error.
I guess that I can't mix variable types? Because it was able to build after I had did three separate println statements.
(Any clarity on this would be greatly appreciated)
I only think this is correct because when I make a statement of just int variables this happens....
class testApp
{
public static void main(String[] args)
{
int x = 6;
int y = 8;
System.out.println(x * y);
}
}
run:
48
BUILD SUCCESSFUL (total time: 2 seconds)
------------------------------------------------------------------------------------------------------------
phew okay, Final Variables (or constants)
this is easy enough to understand, just declaring a variable that can't change after it has been initialized.
this is done by adding final to the variable declaration.
class FINALVARIABLE
{
public static void main(String[] args)
{
final int WEEKDAYS = 5;
System.out.println(WEEKDAYS);
}
}
run:
5
BUILD SUCCESSFUL (total time: 2 seconds)
TO create a final class variable add static before final when declaring a variable.
I doubt there are many still reading this. I still have to mention that this blog is mostly to demonstrate
that I am learning about Java from online resources as well as JFD (Java for dummies) as well as a reference for me in the future.
I'm sorry that this is a long read but, I am still working on a easy to read layout.
No comments:
Post a Comment