Solved

Java does not have global variables or functions

Posted on 2014-03-25
6
270 Views
Last Modified: 2014-04-24
Hi,

I was reading as below
All code in Java program is encapsulated within classes therefore Java does not have global variables or functions.

I have not understood what it means by
Java does not have global variables or functions.

Please advice.
Any links resources ideas highly appreciated. Thanks in advance
0
Comment
Question by:gudii9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 35

Expert Comment

by:mccarl
ID: 39954698
Basically, it just means that you can define a function/variable that is available to your entire application, without it being defined inside a class.

For example, you can't define say a function called "doSomething()" that is available to be called from anywhere in your application using that syntax above. Your only option is to define that function inside a class, eg...
public class ABC {
    public static void doSomething() {
        // do something here
    }
}

Open in new window

And then you call that function by first qualifying it with the class name, ie. ABC.doSomething();

Or in other words, you can't have a function/variable that is NOT associated with a class (or an object of that class)
0
 
LVL 7

Author Comment

by:gudii9
ID: 39995089
For example, you can't define say a function called "doSomething()" that is available to be called from anywhere in your application using that syntax above.

which syntax above?

please advise. I only see code below which is clear.
0
 
LVL 35

Expert Comment

by:mccarl
ID: 39996969
I was referring to the "doSomething()" bit. So to expand, I was basically saying that you can't have a function called "doSomething()" and be able to call that code using a line like....
doSomething();

Open in new window

...from anywhere in your code.
0
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
LVL 7

Author Comment

by:gudii9
ID: 40005167
:

I was referring to the "doSomething()" bit. So to expand, I was basically saying that you can't have a function called "doSomething()" and be able to call that code using a line like....

doSomething();
                                           


within same class we can do that right. please advise with complete example
0
 
LVL 35

Accepted Solution

by:
mccarl earned 500 total points
ID: 40005595
within same class we can do that right
Yes, that is the ONLY way you can do it. But that was the point that I was making, if it is within the class it is therefore NOT global.

please advise with complete example
It's impossible to give you an example, since we are talking about something that you CAN'T do.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40006981
I do not know much about 'Java does not have global variables or functions' if they are c++ or c# terms 'global variables or functions' .

I was thinking ' global variables or functions' means 'Static variables nd static functions' as per my java knowledge. Please correct me if i am wrong.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
login form jsp example 2 74
Java class and jar 3 61
Code Coverage and Coding Standards 2 82
Java pass by reference 3 75
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question