473,395 Members | 1,466 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

private and static error

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
3 5370
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Francesco Monica | last post by:
Hello. This (example) code will compile correctly with gcc <= 3.3.0, but doesn't compile with gcc 3.3.1 (under linux): ------------------------------------------------------------- template <...
12
by: Bryan Parkoff | last post by:
CMain Class is the base class that is initialized in main function. CA Class is the base class that is initialized in CMain::CMain(). CMain Class is always public while CA Class is always...
21
by: Anthony England | last post by:
Everyone knows that global variables get re-set in an mdb when an un-handled error is encountered, but it seems that this also happens when the variable is defined as private at form-level. So...
2
by: Anjo Gasa | last post by:
I have two classes: // B.h class B { friend class A; //... private: static float rate; }
2
by: ma740988 | last post by:
I might be a little off topic but the experience level here is enough to give me solid guidance. So here's the code: #ifndef FOO_TEST_H #define FOO_TEST_H #include <string> #include <deque>...
3
by: John Salmon | last post by:
g++ complains about illegal access to a private member when the following is compiled with a private copy constructor for the class C. When the copy constructor is public, the program runs and...
6
by: VSP | last post by:
Hello, I am just implementing singleton pattern in various ways. In one implementation I created a static member and returning that static member in the getInstance() function. I have made...
6
by: bw171 | last post by:
I got some java code below, and upon trying to compile it in my unix environment, i get the following error ()> javac Downloader.java Downloader.java:37: ';' expected private enum...
1
by: romand | last post by:
EDIT: ***SOLUTION FOUND*** Just to check, before I had the 2 functions in the second code box I used in the main the logger function and when I got it to be private I haven't erased the lines in the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.