473,378 Members | 1,309 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,378 software developers and data experts.

Static variable in method.

dmjpro
2,476 2GB
I m from C and C++ programming world.
There i saw that a static variable in a method lives between two function calls.
Right??
But in JAVA it is valid.
Look at this code carefully.....

Expand|Select|Wrap|Line Numbers
  1. class Sample
  2. {
  3.  //Some code
  4.  void test()
  5.  {
  6.   //Some code
  7.   static int a; //It is not valid ... the compiler error message flashes.
  8.   //Some code
  9.  }
  10.  //Some code
  11. }
  12.  
Plz explain ....

Kind regards,
Dmjpro.
Jun 5 '07 #1
4 1868
blazedaces
284 100+
I m from C and C++ programming world.
There i saw that a static variable in a method lives between two function calls.
Right??
But in JAVA it is valid.
Look at this code carefully.....

Expand|Select|Wrap|Line Numbers
  1. class Sample
  2. {
  3.  //Some code
  4.  void test()
  5.  {
  6.   //Some code
  7.   static int a; //It is not valid ... the compiler error message flashes.
  8.   //Some code
  9.  }
  10.  //Some code
  11. }
  12.  
Plz explain ....

Kind regards,
Dmjpro.
You can't create static variables in a non-static method. In fact, that's probably the EXACT error that showed up when you tried to compile this... am I wrong? What error did show up? I'm curious...

Also, the entire idea of a static variable: they're static, which means they are the same for all instances of that class, means that they should be created as instance variables. It only makes sense. You can set what they are in other methods, but you can't create them there.

Hope that helped,
-blazed
Jun 5 '07 #2
JosAH
11,448 Expert 8TB
Java doesn't implementt that feature. In C/C++ the lexical scope of a local static
variable is the function itself (it's invisible outside that function), but the variable
'lives' forever (as long as the process lives).

You can easily mimic functionally equivalent behaviour using a static class
variable. The variable can be 'seen' in every method of the class but if other
functions promise to stay away from it, the functionality is identical.

kind regards,

Jos
Jun 5 '07 #3
dmjpro
2,476 2GB
You can easily mimic functionally equivalent behaviour using a static class
variable. The variable can be 'seen' in every method of the class but if other
functions promise to stay away from it, the functionality is identical.
These lines r very tough to understand.
Plz tell in simple language.
Plz.

Kind regards,
Dmjpro.
Jun 6 '07 #4
JosAH
11,448 Expert 8TB
These lines r very tough to understand.
Plz tell in simple language.
Plz.

Kind regards,
Dmjpro.
This piece of code using C/C++:
Expand|Select|Wrap|Line Numbers
  1. int foo() {
  2.    static int bar= 0;
  3.    return bar++;
  4. }
Can be written in Java as follows:
Expand|Select|Wrap|Line Numbers
  1. private static bar= 0;
  2. public int foo() {
  3.    return bar++;
  4. }
Using the Java version the variable bar can be manipulated by other class and
instance methods; if they 'promise' to stay away from it the functionality of the
two code snippets is identical.

kind regards,

Jos
Jun 6 '07 #5

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

Similar topics

1
by: James | last post by:
Hello Java NG, I not sure if this is the right NG for this type of question but if not please let me know which is, TIA Any way first off let me say I'm a student and this WAS last weeks lab,...
4
by: Dave | last post by:
I used the following class and .aspx code below to understand how static works on variables and methods taken from...
28
by: Dennis | last post by:
I have a function which is called from a loop many times. In that function, I use three variables as counters and for other purposes. I can either use DIM for declaring the variables or Static. ...
6
by: thomson | last post by:
Hi All, In a Singleton pattern , if we create an instance variable, and return it, whether this one also be a static variable , Can anyone give me insights on the Memory allocation Thanks in...
44
by: John A. Bailo | last post by:
Dr. Dobbs has a /glowing/ article on Ruby on Rails this month. What do you guys think? Can it replace .net, php and java? And be the Open Source OOP web solution that is not bound to Sun or...
8
by: nytimescnn | last post by:
I've read some discuession about lock() for thread-safe. I am wondering what will be the differce between below two code segment? Code 1: class A { private static Object padlock = new...
10
by: Franky | last post by:
I think I misread a post and understood that if I do: System.Windows.Forms.Cursor.Current = Cursors.WaitCursor there is no need to reset the cursor to Default. So I made all the reset...
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...
37
by: minkoo.seo | last post by:
Hi. I've got a question on the differences and how to define static and class variables. AFAIK, class methods are the ones which receives the class itself as an argument, while static methods...
0
by: Joe Strout | last post by:
Hi Luis, A static variable IS encapsulation. Encapsulation happens at many levels: module, class, instance, and (in languages that support it) method. A static local variable is simply the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.