By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,009 Members | 1,487 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,009 IT Pros & Developers. It's quick & easy.

private and static error

P: n/a
public class StaticField2{
public static void main(String args[]){
private int x, y; // <<== error 1
for ( y = 0 ; y < 100 ; y++){
x = StaticMethod();
System.out.println(" x = "+x);
}
}
public static int StaticMethod(){
private static int m = 0; // <<== error 2
m = m++;
return m;
}
}

why i can't declare x and y as a private?
why i cant declare m as private static?

New to Java(OOP?) world.
TIA.
--
"If we don't stand together than this is the end"
The Business

Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
SPG
Hi,

OK, Here is your solution:

<SNIP YOUR CODE>
public class StaticField2{
public static void main(String args[]){
private int x, y; // <<== error 1 (SPG: All variables declared inside a
method are private to that method. JAVA does not allow you to modify this.)
for ( y = 0 ; y < 100 ; y++){
x = StaticMethod();
System.out.println(" x = "+x);
}
}
public static int StaticMethod(){
private static int m = 0; // <<== error 2 (SPG: Cannot keep a static
variable inside a method)
m = m++;
return m;
}
}
</SNIP>

So, To acheive your solution I would imagine you need to do this:

public class StaticField2{
private static int m = 0; // <= Static variable declared global to the
class only.. All methods have access to the single instance

public static void main(String args[]){
int x, y; //<= BOTH ARE LOCAL VARIABLES TO THE MAIN METHOD
for ( y = 0 ; y < 100 ; y++){
x = StaticMethod();
System.out.println(" x = "+x);
}
}
public static int StaticMethod(){
m = m++;
return m;
}
}

I hope this helps you..

Steve

"IHateSuperman" <Wh*@you.care> wrote in message
news:40********@news.tm.net.my...
public class StaticField2{
public static void main(String args[]){
private int x, y; // <<== error 1
for ( y = 0 ; y < 100 ; y++){
x = StaticMethod();
System.out.println(" x = "+x);
}
}
public static int StaticMethod(){
private static int m = 0; // <<== error 2
m = m++;
return m;
}
}

why i can't declare x and y as a private?
why i cant declare m as private static?

New to Java(OOP?) world.
TIA.
--
"If we don't stand together than this is the end"
The Business

Jul 17 '05 #2

P: n/a
IHateSuperman <Wh*@you.care> wrote in message news:<40********@news.tm.net.my>...
public class StaticField2{
public static void main(String args[]){
private int x, y; // <<== error 1
for ( y = 0 ; y < 100 ; y++){
x = StaticMethod();
System.out.println(" x = "+x);
}
}
public static int StaticMethod(){
private static int m = 0; // <<== error 2
m = m++;
return m;
}
}

why i can't declare x and y as a private?
why i cant declare m as private static?


Because x, y, and m are local variables in a method. Keywords
"private" and "static" can only be applied to member variables. A
local variable is already private, in that it's impossible to access
it from outside the method it is declared in. "static" doesn't even
make sense for a local variable; if you're looking for the same
functionality of the "static" keyword in C when applied to a local
variable, declare a member variable instead. Of course, the member
variable will be accessible from all methods of the class, not just
StaticMethod. This should work:

public class StaticField2{
private static int m = 0; // private static class member

public static void main(String args[]){
int x, y; // Local variables
for ( y = 0 ; y < 100 ; y++){
x = StaticMethod();
System.out.println(" x = "+x);
}
}
public static int StaticMethod(){
m = m++;
return m;
}
}
Jul 17 '05 #3

P: n/a
SPC
Hoping it's help!
I am sorry if it's doesn't.

//Start
package Private;

public class Private{

static int x, y; // <<== error 1 //ok
private static int m = 0; // <<== error 2 //ok
// declare sth as private / private static in "Class" but out side "main"
and "fonction" //
public static void main(String args[]){

for ( y = 0 ; y < 100 ; y++){
x = StaticMethod();
System.out.println(" x = "+x);
}
}
public static int StaticMethod(){

m = m++;
return m;
}
}
//END


"IHateSuperman" <Wh*@you.care> a écrit dans le message de
news:40********@news.tm.net.my...
public class StaticField2{
public static void main(String args[]){
private int x, y; // <<== error 1
for ( y = 0 ; y < 100 ; y++){
x = StaticMethod();
System.out.println(" x = "+x);
}
}
public static int StaticMethod(){
private static int m = 0; // <<== error 2
m = m++;
return m;
}
}

why i can't declare x and y as a private?
why i cant declare m as private static?

New to Java(OOP?) world.
TIA.
--
"If we don't stand together than this is the end"
The Business

Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.