473,473 Members | 1,583 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

static local variables

65 New Member
as far as i know we can declare static local variables in C++,
cannot we do the same thing in java?
Sep 28 '07 #1
4 1698
Anand Iyer
6 New Member
as far as i know we can declare static local variables in C++,
cannot we do the same thing in java?

Hi

Whenever you declare a variable it becomes accessible across a class. I dont think you can have a static variable for methods. You can use local variables for methods that live only inside the method. I assume that is what you are meaning by static local methods.

Thanks
Anand Iyer
Sep 28 '07 #2
dmjpro
2,476 Top Contributor
Hi

Whenever you declare a variable it becomes accessible across a class. I dont think you can have a static variable for methods. You can use local variables for methods that live only inside the method. I assume that is what you are meaning by static local methods.

Thanks
Anand Iyer

He is coming from c & c++ background that's why he is asking such a Question.
Let him understand the Java Framework :-)

Kind regards,
Dmjpro.
Sep 28 '07 #3
r035198x
13,262 MVP
as far as i know we can declare static local variables in C++,
cannot we do the same thing in java?
Why don't you try it on the compiler and see what happens?
There's also a few tutorials around (even on this forum) that explain the static keyword in Java.
Sep 28 '07 #4
Nepomuk
3,112 Recognized Expert Specialist
Whenever you declare a variable it becomes accessible across a class.
That's only true, if this variable was declared globally. If you have something like this
Expand|Select|Wrap|Line Numbers
  1. public void myMethod()
  2. {
  3.    int var = 3;
  4. }
  5.  
you won't be able to access var outside of that method.
I dont think you can have a static variable for methods. You can use local variables for methods that live only inside the method. I assume that is what you are meaning by static local methods.
I think what is meant is something like this:
Expand|Select|Wrap|Line Numbers
  1. public void myMethod()
  2. {
  3.    static int var = 3; // This is not allowed in Java
  4.    var++;
  5.    System.out.println(var);
  6. }
  7.  
where the output would be:

4
5
6
7

when called 4 times. But no, it certainly can't be done in that way and I don't think it can be done at all in Java.

Greetings,
Nepomuk
Sep 28 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: IHateSuperman | last post by:
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...
2
by: Steve | last post by:
Is a static method that uses local variables thread safe (eg in a web service) In the following code assuming GetRandomValue() and DoSomethingElse() are thread safe, is the static method thread...
9
by: Bryan Parkoff | last post by:
I have noticed that C programmers put static keyword beside global variable and global functions in C source codes. I believe that it is not necessary and it is not the practice in C++. Static...
3
by: Datta Patil | last post by:
Hi , #include<stdio.h> func(static int k) /* point2 : why this is not giving error */ { int i = 10 ; // static int j = &i ; /* point 1: this will give compile time error */ return k; } /*...
2
by: blue | last post by:
We have an abstract class with all static methods. It makes sense to have it static because there are no member variables and the constructor is empty. Some of the methods update the SQL Server...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
14
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared...
16
by: RB | last post by:
Hi clever people :-) I've noticed a lot of people stating not to use static variables with ASP.NET, and, as I understand it, the reason is because the variable is shared across user sessions -...
2
by: Nagrik | last post by:
Dear Group, The book of Bjarne Stroustrup in chapter 5.4.4 says the following "The word static is one of the most overused words in C and C++. For static data members it has both of the...
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...
1
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...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
0
muto222
php
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.