public class demo
{
public static void main(String[] args)
}
I just started to learn java, I have c++ experience, and the layout confuses me. for example if I declare a function in the demo class would that make it a function or method. also if I want to declare a class can I declare it outside the demo class or it must be inside the demo class.
thank you.
You have already declared it. The thing you are missing is your function body.
public static void main(String[] args)
should be
public static void main(String[] args){
//DO Some Stuff
}
Now here is some additional info:
The main function will be started whenever the application is started and the
String[] args
are the arguments that you are going to pass while starting the application.
You can declare as many functions as you want within your class
public class demo{
public static void main(String[] args){
//Do Some Stuff
}
private void someFunction(){
//Do Some Stuff
}
}
For more you can start learning some basics from the internet. There are tons of tutorials. Hope that helps. :)
you can write code like that
public class Emp{
//Instance variable or class level variable even variable as static
String id;
String name;
//static variable
static int count=0;
{
//non static block
}
static{
// static block
}
public Emp(){
//default constructor
}
//parameterized constructor
public Emp(String id, String name){
this.id=id;
this.name=name;
}
// Non-Static Method
public String getId(){
return id;
}
public String getName(){
return name;
}
//Main method
public static void main(String[] args){
//Instance of class
Emp emp=new Emp("1","Xyz");
System.out.println(emp.getId());
System.out.prinln(emp.getName());
}
}
In Java there are no functions. There are only methods.
You can declare methods inside of class definitions. And methods can be static or not static. Just like in C++.
Also there's no need for header files.
Example:
public class Demo {
// This is a constructor
public Demo() {
}
// This is a non-static method
public void method() {
}
// This is a static method.
// (It's also a special entry point to start the program)
public static void main(String[] args) {
}
}
I think it's safe to say that Java is much simpler than C++.
Ps. I capitalized Demo because according to Java's camel case convention, classes should start with an uppercase letter and methods with lowercase.
Related
So I have two classes:
A Main class with the public static void main(String args[]) method
and a Voice class that accesses a static variable from that class.
Within the main class are methods that are used by itself, and are required to be static along with some of its variables.
So I have a static variable within the Main class (that's created/filled in the public static void main(String args[]) method. That's why this case is special) which the other class should be able to access.
Here is an example of what's happening:
public class Main(){
public static int variable;
/*
Unrelated methods go here.
*/
public static void main(String args[]){
Voice v = new Voice();//This is just here for the code to make sense.
variable = 5;
v.doSomething();
}
}
public class Voice(){
public void doSomething(){
System.out.println(Main.variable);
}
}
Upon calling the doSomething() method in Voice, it leads to a nullPointerException.
I could fix this by passing on the variable variable to the Voice class, but is there a more easy way to fix this in the long run, if for instance, I needed to use more than one static variable from the Main class?
Your code is having syntax error. You can use this
class Main{
public static int variable;
/*
Unrelated methods go here.
*/
public static void main(String args[]){
Voice v = new Voice();//This is just here for the code to make sense.
variable = 5;
v.doSomething();
}
}
class Voice{
public void doSomething(){
System.out.println(Main.variable);
}
}
Output will be 5
You should do as follows
public class Main{
public static int variable;
/*
Unrelated methods go here.
*/
public static void main(String args[]){
Voice v = new Voice();//This is just here for the code to make sense.
variable = 5;
v.doSomething();
}
}
class Voice{
public void doSomething(){
System.out.println(Main.variable);
}
}
I surprisingly find this confusing. I must be missing something.
So I have this simple syntax
public class OMG{
public static void main(String args[]){
int hi=2;
letsDoIt();
System.out.println(hi);
}
public static void letsDoIt(){
hi+=1;
}
}
Obviously this cause an error, since hi is a local variable.
Judging from my experience from python I added this
public class OMG{
public static void main(String args[]){
int hi=2;
letsDoIt();
System.out.println(hi);
}
public static void letsDoIt(){
this.hi+=1;
}
}
Which adds extra error when non-static variable cannot be accessed from a static method.
I added static to hi
public class OMG{
public static void main(String args[]){
static int hi=2;
letsDoIt();
System.out.println(hi);
}
public static void letsDoIt(){
this.hi+=1;
}
}
The compiler scolds me for a illegal expression. I substitute the static with private (which some SO answers, recommend) but same error.
Where's my mistake? Is there any way I can solve this, without making global class?
You cannot declare static variables inside a method because static modifier means that a method or field belongs to the class.
The easiest solution to this problem would be to declare the variable as static class variable. Using this approach, you also need to remove this from this.hi in lestDoIt method. The code would be like this:
public class OMG {
static int hi=2;
public static void main(String args[]) {
letsDoIt();
System.out.println(hi);
}
public static void letsDoIt() {
hi+=1;
}
}
Another solution may be using a non static variable hi. This would need you to also remove the static modifier to the letsDoIt method to access to hi field, because static methods cannot access to instance fields because, as explained above, static means that a method or field belongs to the class and not to a specific object instance of the class.
The solution would be:
public class OMG {
int hi=2;
public static void main(String args[]) {
//note that we have to create a new instance of OMG
//because, again, static methods cannot access to non-static methods/fields
OMG omg = new OMG();
omg.letsDoIt();
System.out.println(omg.hi);
}
public void letsDoIt() {
this.hi+=1;
}
}
More info:
Java Tutorials. Using the this Keyword
Java Tutorials. Understanding Class Members
There are two elements causing your issue.
variable hi must be referenced within a shared context between your main method and your letsDoIt method
because your main method is static, and so your letsDoIt, the will only have visibility on static fields (unless passed an instance as argument, that is)
Hence:
Declare hi as a static field of OMG:
public class OMG {
static int hi;
...
Remove the local variable declaration in your main method.
Reference it with OMG.hi or just hi within a static context, not with this.hi as this implies an instance of Main, which is not visible from a static context
You can not do "this.hi+=1" in a static context and in order to access the hi variable from "letsDoIt()" you have to declare it as a class variable like I did in the code below:
public class OMG{
public static int hi;
public static void main(String args[]){
hi=2;
letsDoIt();
System.out.println(hi);
}
public static void letsDoIt(){
hi+=1;
}
}
Static variables are variables of the class, not its instances. You can't have a static variable inside a method.
To fix this error, move hi outside the main method (keeping it static). Also get rid of the this in letsDoIt().
public class OMG {
static int hi=2;
public static void main(String args[]){
letsDoIt();
System.out.println(hi);
}
public static void letsDoIt() {
hi+=1;
}
}
I have 2 classes the static main class and class B. I'm trying to pass main to B, where there is a method that sets fields.
Can this be done?
If so, could you please provide examples?
public static void main(String[] args) {
ArrayList a = new ArrayList()
class b = new class()
b.update(b);
}
class a {
public void update(ArrayList a) {
//updates the encapsulated arrayList field
}
}
The error message keeps on saying that one is static and the other is non-static, but they should be pointing the same object
I'm not entirely sure what you are trying to do, but here is an example that shows that you can pass an instance of the main class into another class:
public class A {
private String str = null;
public static void main(String[] args) {
A a = new A();
B b = new B(a);
System.out.println(a.getStr());
}
public String getStr() {
return this.str;
}
public void setStr(String str) {
this.str = str;
}
}
public class B {
public B(A a) {
a.setA("hello");
}
}
Running this code will print out hello.
main is static and public, so you can call it from any other class as any other public static method: statically.
if you have a class A that contains a
public static void main(String[] args)
method, then class B can call this method like
A.main(s);
where s is String[]
your question is far from clear, so I suggest you to add more code samples to make it clear what you're really trying to do.
It is said that non-static variables cannot be used in a static method. But public static void main does. How is that?
No, it doesn't.
public class A {
int a = 2;
public static void main(String[] args) {
System.out.println(a); // won't compile!!
}
}
but
public class A {
static int a = 2;
public static void main(String[] args) {
System.out.println(a); // this works!
}
}
or if you instantiate A
public class A {
int a = 2;
public static void main(String[] args) {
A myA = new A();
System.out.println(myA.a); // this works too!
}
}
Also
public class A {
public static void main(String[] args) {
int a = 2;
System.out.println(a); // this works too!
}
}
will work, since a is a local variable here, and not an instance variable. A method local variable is always reachable during the execution of the method, regardless of if the method is static or not.
Yes, the main method may access non-static variables, but only indirectly through actual instances.
Example:
public class Main {
public static void main(String[] args) {
Example ex = new Example();
ex.variable = 5;
}
}
class Example {
public int variable;
}
What people mean when they say "non-static variables cannot be used in a static method" is that non-static members of the same class can't be directly accessed (as shown in Keppils answer for instance).
Related question:
Accessing non-static members through the main method in Java
Update:
When talking about non-static variables one implicitly means member variables. (Since local variables can't possible have a static modifier anyway.)
In the code
public class A {
public static void main(String[] args) {
int a = 2;
System.out.println(a); // this works!
}
}
you're declaring a local variable (which typically is not referred to as non-static even though it doesn't have a static modifier).
The main method does not have access to non-static members either.
final public class Demo
{
private String instanceVariable;
private static String staticVariable;
public String instanceMethod()
{
return "instance";
}
public static String staticMethod()
{
return "static";
}
public static void main(String[] args)
{
System.out.println(staticVariable); // ok
System.out.println(Demo.staticMethod()); // ok
System.out.println(new Demo().instanceMethod()); // ok
System.out.println(new Demo().instanceVariable); // ok
System.out.println(Demo.instanceMethod()); // wrong
System.out.println(instanceVariable); // wrong
}
}
This is because by default when you call a method or variable it is really accessing the this.method() or this.variable. But in the main() method or any other static method(), no "this" objects has yet been created.
In this sense, the static method is not a part of the object instance of the class that contains it. This is the idea behind utility classes.
To call any non-static method or variable in a static context, you need to first construct the object with a constructor or a factory like your would anywhere outside of the class.
More depth:
Basically it's a flaw in the design of Java IMO which allows static members (methods and fields) to be referenced as if they were instance members. This can be very confusing in code like this:
Thread newThread = new Thread(runnable);
newThread.start();
newThread.sleep(1000);
That looks like it's sending the new thread to sleep, but it actually compiles down into code like this:
Thread newThread = new Thread(runnable);
newThread.start();
Thread.sleep(1000);
because sleep is a static method which only ever makes the current thread sleep.
Indeed, the variable isn't even checked for non-nullity (any more; it used to be, I believe):
Thread t = null;
t.sleep(1000);
Some IDEs can be configured to issue a warning or error for code like this - you shouldn't do it, as it hurts readability. (This is one of the flaws which was corrected by C#...)
**Here you can see table that clear the access of static and non-static data members in static and non-static methods. **
You can create non-static references in static methods like :
static void method() {
A a = new A();
}
Same thing we do in case of public static void main(String[] args) method
public class XYZ
{
int i=0;
public static void increament()
{
i++;
}
}
public class M
{
public static void main(String[] args)
{
XYZ o1=new XYZ();
XYZ o2=new XYZ();
o1.increament();
XYZ.increament(); //system wont be able to know i belongs to which object
//as its increament method(static method)can be called using class name system
//will be confused changes belongs to which object.
}
}
I have a simple problem that I've been stuck on for some time and I can't quite find the answer to. Basically, I'm creating an object and trying to access the variables without using static variables as I was told that is the wrong way to do it. Here is some example code of the problem. I receive an error in the first class that can not be resolved to a variable. What I would like to be able to do is access t.name in other methods outside of the main, but also in other classes as well. To get around this previously I would use Test2.name and make the variable static in the Test2 class, and correct me if I'm wrong but I believe that's the wrong way to do it. Any help would be greatly appreciated =)
public class Test {
public static void main(String[] args) {
Test2 t = new Test2("Joe");
}
public void displayName() {
System.out.println("Name2: " + t.name);
}
}
public class Test2 {
String name;
public Test2 (String nm) {
name = nm;
}
}
I see others have posted code snippets, but they haven't actually posted why any of this works (at the time of this writing.)
The reason you are getting a compilation error, is that in your method
public static void main(String[] args) {
Test2 t = new Test2("Joe");
}
Variable t's scope is just that method. You are defining Test2 t to only be in the main(String[] args) method, so you can only use the variable t in that method. However, if you were to make the variable a field, like so, and create a new instance of the Test class,
public class Test {
Test2 t;
public static void main(String[] args) {
Test test = new Test();
test.t = new Test2("Joe");
test.displayName();
}
public void displayName() {
System.out.println("Name2: " + t.name);
}
}
Then you should no longer be getting any compilation errors, since you are declaring the variable t to be in the class Test scope.
You may give the reference to your test object as an argument to method displayName:
public class Test {
public static void main(String[] args) {
Test2 t = new Test2("Joe");
displayName(t);
}
public static void displayName(Test2 test) {
System.out.println("Name2: " + test.name);
}
}
Note: I also made displayName a static method. From within your main method you can only access static methods without reference.
Modify the Test class to this
public class Test {
private static Test2 t;
public static void main(String[] args) {
t = new Test2("Joe");
}
public void displayName() {
System.out.println("Name2: " + t.name);
}
}
Use a getter for your purpose. This is a side solution to your problem, but generally this is how you should retrieve instance variables, using getters.
public class Test {
public static void main(String[] args) {
Test2 t = new Test2("Joe");
displayName(t);
}
public static void displayName(Test2 test) {
System.out.println(test.getName());
}
}
public class Test2
{
private String name;
public Test2 (String nm)
{
name = nm;
}
public String getName()
{
return name;
}
}
Always remember, variables in your class should be private. That protects it from access from outside the class. Hence, getters are the only way to access them. And setters or constructors to initialize them.
Fewer statics the better I reckon.
I would Instantiate Test and call displayName on the instance of it, then pass the instance of Test2 to displayName.
But it does depend on what the overall aim is