473,472 Members | 2,181 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

non static variable

sanjay123456
125 New Member
Dear Friends
Expand|Select|Wrap|Line Numbers
  1.  
  2. public class hello
  3. {
  4.           int i=0;
  5.           public static void main(String args[])
  6.           {
  7.                 System.out.print(i);
  8.             }
  9. }
  10.  
  11.  
can it possible that i can use int i in main function without declaration as static ?

sanjay
Feb 4 '07 #1
6 2598
horace1
1,510 Recognized Expert Top Contributor
you either make it static or you access it via an object of type hello
Expand|Select|Wrap|Line Numbers
  1. public class hello
  2. {
  3.           int i=0;
  4.           int geti() { return i; }
  5.           public static void main(String args[])
  6.           {
  7.                 hello h=new hello();
  8.                 System.out.print(h.geti());
  9.             }
  10. }
and each hello object will have its own copy of i
Feb 4 '07 #2
r035198x
13,262 MVP
Find time to read this.
Feb 5 '07 #3
sanjay123456
125 New Member
Thx dear

i have a problem that '

why java accept a varoable as static in only class level

we did not declare a variable as static in function level ?

sanjay
Feb 5 '07 #4
r035198x
13,262 MVP
Thx dear



i have a problem that \'



why java accept a varoable as static in only class level



we did not declare a variable as static in function level ?



sanjay


If a method is static, all the variables declared in that method are implicitly static. Also you cannot declare static variables inside non-static methods.
Feb 5 '07 #5
horace1
1,510 Recognized Expert Top Contributor
Thx dear

i have a problem that '

why java accept a varoable as static in only class level

we did not declare a variable as static in function level ?

sanjay
are you thinking of static local variables in C functions, e.g.
Expand|Select|Wrap|Line Numbers
  1. int test()
  2. {
  3.    static int x=0;
  4.    x++;
  5.   return x;
  6. }
  7.  
x is initialised to 0 in the first function call and then incremented on sucessive calls - it retains its value between calls unlike 'normal' local variables the value of which is lost and reinitialised on each call (if it has an inital value otherwise it is undefined), Sucessive calls of test() return 1, 2, 3, etc.
Feb 5 '07 #6
dmjpro
2,476 Top Contributor
static variables are those variables which can be accessed without instantiate the corresponding class.
here the main class u used jvm didn't instantiate..
the static members of a class are used only in static function of that class.
Feb 5 '07 #7

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

Similar topics

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; } /*...
9
by: AnandRaj | last post by:
Hi guys, I have a few doubts in C. 1. Why static declartions are not allowed inside structs? eg struct a { static int i; }; Throws an error ..
5
by: Tom Pearson | last post by:
What is the scope of static variable when programming in ASP.NET? For example I have a control class that uses static callbacks so that another window can pass a list of items to it. The control...
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: Vladislav Kosev | last post by:
I have this strange problem now twice: I am writing this relatevely large web site on 2.0 and I made a static class, which I use for url encoding and deconding (for remapping purposes). This static...
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...
9
by: Jess | last post by:
Hello, I was told that if I declare a static class constant like this: class A{ static const int x = 10; }; then the above statement is a declaration rather than a definition. As I've...
10
by: Pramod | last post by:
Hello to all of you, I want to know that what's the use to create static object. Thanks You Pramod Sahgal
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...
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 -...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...
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...
0
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,...
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: 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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
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.