score:0
its because gettag()
is not a static method.
try this
datum dt = datum.heute();
system.out.println(dt.gettag());
score:0
your gettag()
method is not static, so you cannot call/invoke that method without an object , that's why you are getting that error.
what you can do is assign a variable to the object returned by datum.heute()
and then invoke gettag()
on that object
you should do it like this :
datum d = datum.heute();
system.out.println(d.gettag());
score:0
this line in your code is causing the problem:
system.out.println(datum.gettag());
gettag()
method is non-static and cannot be referenced directly in the main
method without creating object of datum
.
solution:
system.out.println(datum.heute().gettag());
score:0
your heute()
method is static
and hence the use is proper, but to call the gettag()
you need to use the object returned from heute()
method.
datum date = datum.heute();
system.out.println(date.gettag());
score:0
if you want to print todays date, you could
public static void main(string[] args) {
system.out.println( datum.heute().gettag() );
}
datum.heute()
is a static (class) method which returns a datum
(object) on which you, in turn, can call the non-static datum
methods. makes sense?
cheers,
score:0
your datum class method gettag()
is an instance method, and you are trying to call it with class name, which is not possible because instance methods instantiated after object creation, and only static members instantiated after class load.
so, you have to create an object of your datum class to call gettag()
method:
datum d = datum.heute();;
system.out.println(d.gettag());
score:0
you can create an object of the class datum to get non-static access :
public class aufgabe3 {
public static void main(string[] args) {
datum datum = new datum()
system.out.println(datum.gettag());
}
}
score:0
make an object of your datum class like this in main method. datum datum = new datum(); and then try using datum.gettag();
note:- it is just because you have declared all your variables non-static you can't access them without an object directly using class name(as you are doing right now because they are not declared as static.)
score:0
from your line in exception cannot make a static reference to the non-static method gettag() from the type datum
it self say that to you cant not access non-static method directly by class name (like accessin any static method directly by class name), to access non-static method you need reference/object of that class.
create object of your class datum
and access method gettag()
using that object.
public static void main(string[] args) {
datum obj = datum.heute();
system.out.println(obj.gettag());
}
may this help you.
score:1
i try to answer this question by explaining what your code actually does.
by executing datum.heute();
you make a call to the static method heute()
of your datum
class. a static method is independent of an actual instance, i.e. an object of that class.
heute()
returns an instance of datum, but you never store it anywhere.
then in your second line you try to make a call to datum.gettag()
. since you make a call to the class datum and not to an object (which would be somedatumobject.gettag()
for example), the java compiler tries to find a static method.
so to solve your aufgabe 3 you need to:
- create an instance through
heute()
- call
gettag()
on that object
Source: stackoverflow.com
Related Query
- Use function from other class without modifying it (static reference to the non-static method)
- android: use project's final static variable from inside the library, or some other tricks to exclude unused code
- (eclipse) is it okay to use other class in the same folder without importing?
- use a class from the other child module in maven
- Java: cannot access static variable from a different class in the same package
- How to use virtual class in c++ to call method from other class?
- I'm trying to open a class (FlappyBird.java) from the class (Starting.java) but i cant use "new game().setvisible(true)"
- Is it feasible to extract the code assist/completion part from Eclipse and use it in an other editor?
- Why does my eclipse IDE run without compiler error when I create a non static inner class with static methods and variables?
- Eclipse EMF - Is there a way to reference a class that is part of a model in a different eclipse plugin without having to store the tostring()?
- Tycho does not use target file from eclipse-target-definition project without version in the pom
- Undefined reference to function from static pointer
- Android : Unable to Access Function From Other Class (Service)
- Cannot make a static reference to the non-static method setBackground(Color) from the type Component
- Java - How to use a variable from a method when imported into the main class
- Kotlin class on eclipse acting weird, every time I run the program I am getting output from other file
- What does it mean to use the * operator before a reference to a class in this example
- How to display my Inputted Text from the other class
- How to use the class from a jar file into a JSP page?
- HBox setMargin error(the static method set margin (node, insects) from the type HBox should be accessed in a static way) for class assignment
- how to add Interstitial AdMob out MainActivity in other Class without the Method onCreate?
- GWT Eclipse: using classes from other projects cause "Server class ... could not be found in the web app, but was found on the system classpath
- Error instantiating servlet class in the project which uses classes from the other project
- Is there a way to update an object from the main class to all the other classes within the program?
- Cannot make a static reference to the non-static method show(FragmentManager, String) from the type DialogFragment
- Create a new java package in eclipse (apart from default package), but can not use the class from external library in the files in this new folder
- Cannot make a static reference to the non-static method setOnTouchListener(View.OnTouchListener) from the type View
- How to use an int from one class and compare it to int's from other classes?
- Eclipse 3.4 GWT 1.6 project - how to reference source from other projects?
- How to exclude classes from the coverage calculation in EclEmma without actually excluding them from the coverage itself
More Query from same tag
- VS2008 plugin error/warning markers like in eclipse
- android - Unable to instantiate activity - ClassNotFound - Fails on one eclipse but not other
- Java Error: Could not find or load main class. (Eclipse Juno) with package
- Apache Storm Problem: "Unable to canonicalize address localhost/<unresolved>:2000 because it's not resolvable"
- Trying to understand what gradle and maven actually do
- The console in Eclipse (Java) does not output things in the correct/chronological order
- the codes are not working in the second xml file
- Organizing JavaScript code to be visible in Eclipse Outline
- Eclipse watch expression?
- In Java, Why cannot I insert an integer more than once, cannot print out after inserting one integer and cannot empty array with imported libraries?
- How to programmatically run an Xpand workflow on a model in a second workbench?
- Can I addCommand a file and commit it?how?
- Exporting EAR file in Eclipse taking long time
- App not deploying in AVD via Eclipse?
- ndk-gdb cannot find gdb.setup but it is there under x86
- Eclipse does not build C++ files
- Eclipse - format java class/interface declaration
- Activiti hello world test
- JUnit uses wrong Base64
- Tomcat fails deploying and old webapp
- Python module logging with eclipse
- log4j2 files created but not written
- Redirect to an new Activity after login in to Facebook (Facebook SDK 3.5.2)
- How to program collaboratively with Eclipse
- Does "go to definition" work in Eclipse for javascript?
- Getting error at compile time in Eclipse
- Android Eclipse can't see library's class
- Run java app created in eclipse package structure in ubuntu terminal
- How to copy a django project from one folder location to another?
- Alfresco - Using APIs in a new project