Results 1 to 14 of 14
Thread: global declaration in java
- 02-11-2009, 11:20 AM #1
Member
- Join Date
- Feb 2009
- Posts
- 9
- Rep Power
- 0
- 02-11-2009, 11:21 AM #2
Member
- Join Date
- Feb 2009
- Posts
- 9
- Rep Power
- 0
public class Globalvar{
public static MyCustomer objarrcustomer[];
}
this is the way to declare a global variable?
- 02-11-2009, 11:27 AM #3
Member
- Join Date
- Feb 2009
- Location
- Italy
- Posts
- 51
- Rep Power
- 0
depends all on what you intend for global
1) public static Object obj
this variable is visible from outside this class and is static --> it is a variable of the class, not of the instance of that class (it's the same for every instance)
you can access it by ClassName.obj
2) public Object obj
this variable is visible from outside this class and is not static --> it is a variable of the instance of the class (it's different for every instance)
you can access it by:
ClassName instance = new ClassName();
instance.obj
3) the best options is to make your variables private (not visible outside of the class) and use Getters and Setters to access it
Java Code:public class ClassName { private Object obj; public Object getObject(){ return obj; } public void setObject(Object obj){ this.obj = obj; } }
- 02-11-2009, 11:37 AM #4
Member
- Join Date
- Feb 2009
- Posts
- 9
- Rep Power
- 0
can u tell me how to declare a global variable?
- 02-11-2009, 11:44 AM #5
Senior Member
- Join Date
- Dec 2008
- Location
- Kolkata
- Posts
- 286
- Rep Power
- 13
There is nothing global in java. In java you have 4 type of access specifiers
1. default or package level - Accessible in any class within the same package
2. private - Accessible within the class where it has been declared, no where else
3. protected - Accessible in any class within the same package and in any subclass in some other package
4. public - Accessible in any class in the same or different package.
- 02-11-2009, 11:57 AM #6
Member
- Join Date
- Feb 2009
- Posts
- 9
- Rep Power
- 0
thank u ,
but i am doing one program,
in that ther are 3 class library,global var,libraian i stored in different files and accessing by package and import..
in globalvar i am declaring the globalvariable
so declare a global variable array,
public static Mycustomer objArrcustomer[];
Mycustomer is one of my class, and i am trying to store the customername in objArrcustomer[]
ao i want to know wat i declare is right or wrong
- 02-11-2009, 12:02 PM #7
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,568
- Rep Power
- 15
Once again, for slow learners, there is no such thing as a global variable in Java.
Now, you have a public static variable and it can be used, but you need to define it and set it's length before you can start setting element values.
- 02-11-2009, 12:02 PM #8
Senior Member
- Join Date
- Dec 2008
- Location
- Kolkata
- Posts
- 286
- Rep Power
- 13
Yes, it looks ok.
- 02-11-2009, 12:08 PM #9
Member
- Join Date
- Feb 2009
- Location
- Italy
- Posts
- 51
- Rep Power
- 0
I see you didn't read or understand what we said to you
you can declare and access normally a static array like you do now....
and you can access it from everywere with
Globalvar.objArrcustomer
it's not the best way but you can do it
what we try to do is to teach you how can variables be declared and accessed in Java, and not only "if what you declared is "global" or not"
in java there's no GLOBAL thing....
i thought i was understandable when i explained earlier
- 02-11-2009, 03:43 PM #10
Member
- Join Date
- Feb 2009
- Posts
- 9
- Rep Power
- 0
i got it, thank u to all
- 02-11-2009, 04:33 PM #11
Member
- Join Date
- Feb 2009
- Posts
- 9
- Rep Power
- 0
can u give me a program for ...
in a libraray, "the libraian has to register the customer"
using array simple program
- 02-11-2009, 06:10 PM #12
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,568
- Rep Power
- 15
We are not a homework serice.
Try Rent A Coder: How Software Gets Done -- Home of the worlds' largest number of completed software projects
- 02-12-2009, 04:49 PM #13
Member
- Join Date
- Feb 2009
- Posts
- 9
- Rep Power
- 0
i stored a name in an array ,if i want to erase a name and i want to use that memory for storing ....how should i do it
- 02-12-2009, 10:25 PM #14
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,568
- Rep Power
- 15
Similar Threads
-
How to undefine a Global Variable in JAVA?
By ajaykushwaha in forum New To JavaReplies: 13Last Post: 11-19-2008, 10:53 PM -
Declaration
By asifahmed in forum New To JavaReplies: 1Last Post: 04-05-2008, 05:38 AM -
Global constants
By Java Tip in forum Java TipReplies: 0Last Post: 02-17-2008, 09:06 AM -
Declaring global variables
By eva in forum New To JavaReplies: 3Last Post: 12-23-2007, 12:11 AM -
JSP Declaration Directive
By Java Tip in forum Java TipReplies: 0Last Post: 12-10-2007, 05:42 PM
Bookmarks