What's the difference between global variables and instance variables?

hi im just a biginner,
but what is the difference between these two?
both i declare them above the constructor right.
and both can access by any method in the class but my teacher said
global variables are not permitted in java....
but i don't know what that means....and i got started to confuse these two types,,
im confusing.......
and why my teacher said declaring global variables is not permitted,,,,,,
why.....

instance variables are kindof like Global variables. I'm not surprised you are confused.
The difference is not in how they are declared, but rather in how they are used.
There are two different "styles" of programming
- procedural programming.
- object oriented programming.
Global variables are a term from Procedural programming.
In this style of programming, you have only one class, and one "main" procedure. You only create one instance of the class, and then "run" it.
There is one thread of control, which goes through various methods/procedures to accomplish your task.
In this style of programming instance variables ARE "global" variables. They are accessible to all methods. There is only one instance of the class, and thus only one instance of the variables.
Global variables are "bad" BECAUSE you can change them in any method you like. Even from places that shouldn't have to. Also if you use the same name as a global variable and a local variable, you can cause great trouble. This can lead to very subtle bugs, as the procedures interact in ways you don't expect.
The preferred method in procedural programming is to pass the values as parameters to the methods, and only refer to the parameters, and local variables. This means that you can track exactly what your method is doing, and what it affects. It makes it simpler to understand. If you use global variables in your methods, it becomes harder to understand.
So when are instance variables not global variables?
When you are actually using the class as an Object, rather than just a program to run. If you are creating multiple instances of an object, all with different values for their instance variables, then they are not global variables. For instance you declare a Person object with an attribute "firstname". Your "main" program then creates many instances of the Person object, each with their own "firstname"
I guess at the end of all this, it comes down to definitions.
Certainly you can write procedural code in java. You can treat your instance variables, for all intents and purposes like global variables.
I can only think to show a sort of example
public class Test1
   User[] users;
   public void printUsers(){
     // loop through and print all the users
     // uses a global variable
      for(int i=0; i<users.length; i++){
        users.printUser();
public void printUsers(User[] users){
// preferred method - pass it the info it needs to do the job
for(int i=0; i<users.length; i++){
users[i].printUser();
public Test1(){
User u1 = new User("Tom", 20);
User u2 = new User("Dick", 42);
User u3 = new User("Harry", 69);
users = new User[3];
users[0] = u1;
users[1] = u2;
users[2] = u3;
printUsers();
printUsers(users);
public static void main(String[] args)
new Test1();
class User{
String firstName;
int age;
public User(String name, int age){
this.firstName = name;
this.age = age;
public void printUser(){
// here they are used as instance variables and not global variables
System.out.println(firstName + " Age: " + age);
Shit thats a lot of typing, and I'm not even sure I've explained it any good.
Hope you can make some sense out of this drivel.
Cheers,
evnafets

Similar Messages

  • In the shared variable properties box, when enabling aliasing, what is the difference between binding to a project variable vs. to a psp url

    In the shared variable properties box, specifically with regard to a the target being an rt target, when enabling aliasing, what is the difference between binding to a project variable vs. to a psp url?

    The difference is in the type of variable you are binding it to.  A project variable is a variable that lives within the specific project in which it was created.  A PSP URL is an address using the Publish-Subscribe Protocol (PSP).  You can learn more about PSP URLs here: http://zone.ni.com/reference/en-XX/help/371361G-01/lvconcepts/using_datasocket_technology/#Specifyin...
    PSP URLs will enable it to communicate between different machines and also between projects which may or may not be open.  The project variable will exist within the project.
    Julian R.
    Applications Engineer
    National Instruments

  • What are the differences between Captivate 6 and 7?

    What are the differences between Captivate 6 and & 7?

    Check the Captivate blog. BTW there were 3 versions of 6 (each with different features), and already 2 versions of 7.
    http://blogs.adobe.com/captivate/2013/12/the-most-awaited-enhancements-in-advanced-actions -and-variables-are-here.html    explains the new features in 7.0.1.
    You can also find articles on the features of 7.

  • What is the difference between live auction and bid invitation

    hi friends
    what is the difference between live auction and bid invitation
    thanks
    regards
    vinaykrishna

    Source SAP.    
    BI including Price and non-price variables but LAC Bidding is based on price only.
    BI - Longer response time for Bidders (from days to week) but Bidding short bidding windows (Hours).
    BI - Bidders sees no information on competitors' bid but in LAC Rank,Best Bid , and next bid can be displayed on real time
    BI-Well suited for large variety of goods and services , including ram waterials, services and projects but LAC More suited for commodities or goods and services with standard attributes that vary only slightly from supplier to supplier.
    BI -Sourcing is based on value propositions (.eg. High Quality ,fast deliveries) rather than price but LAC market dynamics of auctions work best when buyer/seller relationship is loosele- coupled.
    BR
    Raman

  • What is the difference between message type and element

    hi,
    When we create a new variable in BPEL we get the option to create 'simple', 'message type', 'element'
    I would like to know what is the difference between message type and element.
    thanks
    Yatan

    A message is part of the service and is usually assigned to one of the operations in the wsdl. For example getPhoneNumer() operation may have an input message of personInput and output of phoneNum. These messages would have a type...maybe of string or decimal or a complex type with multiple values.
    An element is typically a complex type that could be defined in an xsd file.
    When you create a variable in the bpel, you can declare it as a message or a simple type or an element. As I understand it, the simple types are pre-defined in the schema namespace such as string, decimal, date, etc.
    The elements are defined in the xsd files or wsdls and can be a combination of strings, dates, etc or other stuff as well.
    The messages are defined in the wsdl as part of the input/output but they are typically assigned a type just like other variables you create.

  • What is the difference between access specifiers and access modifiers?

    what is the difference between access specifiers and access modifiers? are they same? if not what is the difference.

    Access Specifier are used to specifiy how the member variable ,methods or class to other classes.They are public ,private and protected.
    Access Modifier:
    1.Access
    2.Non Access
    Access:
    public ,private,protected and default.
    Non Access:
    abstract,final,native,static,synchronized,transient,volatile and strictfp

  • What is the difference between Topic Keywords and Index File Keywords?

    What is the difference between Topic Keywords and Index File Keywords? Any advantages to using one over the other? Do they appear differently in the generated index?
    RH9.0.2.271
    I'm using Webhelp

    Hi there
    When you create a RoboHelp project you end up with many different ancillary files that are used to store different bits of information. Many of these files bear the name you assigned to the project at the time you created it. The index file has the project name and it ends with a .HHK file extension. (HHK meaning HTML Help Keywords)
    Generally, unless you change RoboHelp's settings, you add keywords to this file and associate topics to the keywords via the Index pod. At the time you compile a CHM or generate other types of output, the file is consulted and the index is built.
    As I said earlier, the default is to add keywords to the Index file until you configure RoboHelp to add the keywords to the topics themselves. Once you change this, any keyword added will become a META tag in the topic code. If your keyword is BOFFO, the META tag would look like this:
    <meta name="MS-HKWD" content="BOFFO" />
    When the help is compiled or generated, the Index (.HHK) file is consulted as normal, but any topics containing keywords added in this manner are also added to the Index you end up with. From the appearance perspective, the end user woudn't know the difference or be able to tell. Heck, if all you ever did was interact with the Index pod, you, as an author wouldn't know either. Well, other than the fact that the icons appear differently.
    Operationally, keywords added to the topics themselves may hold an advantage in that if you were to import these topics into other projects, the Index keywords would already be present.
    Hopefully this helps... Rick

  • What is the difference between Video-out and mirroring?

    What is the difference between Video-out and mirroring? I can't get iPhone 4 video to work on my TV screen
    I have just bought an MD098ZM/A (Apple 30-pin Digital AV Adapter). I am struggling to get it to show a picture on my TV. I know I'm doing something right because the audio is coming out of my TV speakers but no picture on the TV screen.
    I have used the same HDMI channel (on the TV side) with the same cable and my thunderbolt port (MacBook Air) without any trouble - and on the same app (BBC iPlayer download then full-screen mode).
    Now I note that the packaging for the MD098ZM/A says video-out on iPhone 4 but mirroring only on iPhone 4S. I only have an iPhone 4 (not the 4S). Now if the lack of iPhone 4 support for mirroring means that I can't play video material out to my TV, then in what sense is there any video-out capability at all?
    There is only safety and warranty paperwork in the Apple adapter packaging - no help information. And I haven't found further guidance online either.
    I do note somewhere online that it suggests that basic non-mirroring video-out (for this adapter) only works with some external TV sets. Any way of finding out which? I'm using a Sanyo CE32LD90-B LCD TV if it helps.
    So far not doing very well.

    Now found these but have had to give up on this adapter!
    http://manuals.info.apple.com/en_US/iphone_user_guide.pdf
    http://support.apple.com/kb/HT4108

  • What is the difference between jsp :include and server side include

    what is the difference between jsp :include and server side include(request dispatcher include method)????
    i understand that both request dispatcher include method and jsp:include take dynamic data,so when would one use request dispatcher include and when jsp:include.
    Is the usage interchangeable?i believe jsp include is used only for jsp/html but include directive can be used to include servlets ,jsp and html....correct me if i m wrong and
    do suggest if u hav ny other diff in this context...

    The difference really is: in what format do you want your inclusions? If your environment has many Java developers and only a few designers that focus mainly on, say, Flash, that might push you more towards the server-side include() directive. Or, if you have a large set of pages that receive dynamic content that is displayed in a consistent fashion (such as a workflow header area on a page).
    If, on the other hand, you have more web designers, there may be a greater desire to deal in markup rather than Java code. Java developers themselves might prefer to view markup (JSP) that more resembles the eventual output than something occuring in Java code.
    Finally, there are considerations of tiering. While it is totally possible to (and I have previously) implement 'view classes' that render markup or generate layout templates, JSP's offer, IMO, a subtle, psychological advantage. By forcing a developer to work in a different format, markup versus Java source, the separation on view from controller and model becomes a bit easier. It is still possible to make mistakes, but if a developer at some point notices, "Wait, I'm in a JSP, should I be importing a java.sql class?", then the choice to use JSP includes has paid off in spades.
    - Saish

  • What is the difference between Session timeout and Short Session timeout Under Excel Service Application -- session management?

    Under Excel Service Application --> session management; what is the difference between Session timeout and Short Session timeout?

    Any call made from the API will automatically be set to the “Session Timeout” period, no matter
    what. Calls made from EWA (Excel Web Access) will get the “Short Session Timeout” period assigned to it initially.
    Short Session Timeout and Session Timeout in Excel Services
    Short Session Timeout and Session Timeout in Excel Services - Part 2
    Sessions and session time-outs in Excel Services
    above links are from old version but still applies to all.
    Please remember to mark your question as answered &Vote helpful,if this solves/helps your problem. ****************************************************************************************** Thanks -WS MCITP(SharePoint 2010, 2013) Blog: http://wscheema.com/blog

  • What are the differences between ECC5.0 and 6.0 new GL functionality

    Hi Experts,
    What are the differences between ECC5.0 and 6.0 new GL functionality.
    If we want to implement IFRS, I think New GL functionality is very helpful, but why sap is recommending only for ECC6.0 new GL functionality, Eventhough this functionality is available in ECC5.0.
    Best Regards,
    Dharani

    Dear,
    ECC 5.0 vs. ECC 6.0/SAP ERP 6.0
    Functionality not available in the new G/L with mySAP ERP 2004 and ERP Central Component (ECC) 5.0:
    Transfer prices
    Statistical key figures
    Euro translation
    Audit Information System (AIS)
    Archiving
    Data retention tool
    Regards,
    Chintan Joshi.

  • What is the difference between document class and normal class

    Hi,
    Please let me know what is the difference between document class and normal class.
    And I have no idea which class should call as document class or call as an object.
    Thanks
    -Actionscript Developer

    the document class is invoked immediately when your swf opens.  the document class must subclass the sprite or movieclip class.
    all other classes are invoked explicitly with code and need not subclass any other class.

  • In OSX Activity Monitor, what's the difference between 'free' memory and 'inactive' memory

    In OS X Activity Monitor, what's the difference between 'free' memory and 'inactive' memory. My daughters 2Gb MacBook Pro is very slow, it probably needs more memory but there is almost 1Gb of 'inactive' memory but no 'free' memory when an application is opened.
    Thanks

    Free RAM is the one that has not been used by any application since you started up your Mac, and inactive RAM is the one that was used by an application and it is not in use anymore.
    1 GB of inactive RAM is a lot, and it may be the cause of the slowness. There are a lot of apps that allows you to turn inactive into free RAM. I use FreeMemory, but have a look at the Mac App Store.
    If you are a developer, you can do that with a command you can type into Terminal:
    sudo purge

  • What is the difference between local printing and remote printing in SAP

    Hello Nation,
    I would like to know what is the difference between local printing and remote printing in SAP and in what scenarios they are used ?
    1.What is local printing in SAP ?
    2.What is Remote Printing in SAP?
    3.what is the difference between local printing and remote printing in SAP ?
    Thank you in Advance.
    Regards,
    -Mohamed Naveen.

    Hi,
    This is SAP Business one system administration forum. Please find correct forum and repost your discussion to get quick response.
    Please close this thread here with helpful answer.
    Thanks & Regards,
    Nagarajan

  • What is the difference between Business System and Business Service?

    Hi
    Please tell me what is the difference between Business System and Business Service...? In real time at what situation we will use Business System and in what situations we will use Business Service..? Please help me
    Best Regards
    Ravi Shankar B

    HI,
    Business system:
    If you want to address a particular business system as the sender or receiver of messages, choose this service type.
    A business system is an actual application system in a system landscape. A business system (service) comprises information about the inbound and outbound interfaces and the software component versions of the business system.
    You usually use business system services when configuring internal company processes.
    Business Service:
    If you want to address an abstract business entity as the sender or receiver of messages, choose this service type.
    Using a business service, you can define the technical or business subunits of the companies involved and then assign them the relevant interfaces.
    You usually use business services when configuring cross-company processes. In this case, you only make your interfaces known to the business partners involved and either do not make any details about your own system landscape available, or only specific details. 
    For example, you can define RosettaNet Partner Interface Processes (PIPs) as business services.
    Regards,
    Sudheer.

  • What is the Difference between AQ adpater and JMS adapter??

    What is the Difference between AQ adpater and JMS adapter in BPEL11g??
    In what scenarios we go for AQ and for what scenarios we go for JMS adapter???

    Go for AQ when using Oracle Advanced Queueing (the database stuff).
    Use JMS when using JMS (in memory of file queues)
    hth,
    Peter Paul

Maybe you are looking for