473,761 Members | 3,187 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.prin tln(" 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 5395
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.prin tln(" 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.prin tln(" x = "+x);
}
}
public static int StaticMethod(){
m = m++;
return m;
}
}

I hope this helps you..

Steve

"IHateSuper man" <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.prin tln(" 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.m y>...
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.prin tln(" 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.prin tln(" 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.prin tln(" x = "+x);
}
}
public static int StaticMethod(){

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


"IHateSuper man" <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.prin tln(" 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
2392
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 < typename T > class Singleton   {   private:
12
3255
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 private. I have placed "friend void CA::Run_A(void)" in CMain Class. CMain::Run() function attempts to execute CA::Run_A(), but compiler shows an error saying that it is the violation to access private function. I don't understand why because friend...
21
4422
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 if "global variables get re-set" doesn't tell the whole story, then what does? ***please note*** I'm not looking for a solution - I'm looking for a more detailed description of what happens when an un-handled error occurs - possibly with help file...
2
3140
by: Anjo Gasa | last post by:
I have two classes: // B.h class B { friend class A; //... private: static float rate; }
2
7980
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> #include <iostream>
3
3399
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 demonstrates(?) that the copy constructor is never called (at least no sign of its chatter on std::cout is visible). So - is it necessary to have a public copy constructor in order to use "function style" initializers for an array of objects? ...
6
2204
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 constructor as private and destructor as public. class Single { private:
6
4385
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 ReportFormat { ^ 1 error This is a portion of the code below
1
6860
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 main. ********* Question: hello, If you remember me from yesterday, having some problems. I've defined a helpFunctions.cpp class with static functions. now I have a static function that I don't need the user to see so I'm trying to make it...
0
9353
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10123
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9909
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9788
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8794
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6623
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5384
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3889
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3481
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.