0

Class1.java

public List<UMRDTO> getDocumentationList(Session session)
{
    List<UMRDTO> documentationList = null;

    try
    {
        Query query = null;
        query        = session.createQuery(UMRSQLInt.DOCUMENTATION_LIST);
        documentationList = query.list();
    }

    return documentationList;
}

I need to use the documentationList that is returned to a static method in this like but getting error like non static method cannot be refrenced from static context

class2.java

static
{
UMRMetadataSupportDAOImpl d=new UMRMetadataSupportDAOImpl();
    listDocuments= d.getDocumentationList(); //error here
    for (UMRDocumentationDTO listDoc: listDocuments)
    {
        if(listDoc.equals(MMTConstantsInt.DOMAIN_NAME))
            domainDocumentationMap.put(listDoc.getId().getObjectName(), listDoc.getDocumentationLink());
        else
            domainComboDocumentationMap.put(listDoc.getId().getObjectName(), listDoc.getDocumentationLink());
    }
deepak mann
  • 111
  • 7
  • Well yes, so you need an instance of your first class, in order to call an instance method... – Jon Skeet Feb 09 '17 at 11:27
  • Please read the answers to some of the [many questions](http://stackoverflow.com/search?q=%22non-static+method+cannot+be+referenced+from+a+static+context%22) about the same error. – Jon Skeet Feb 09 '17 at 11:28
  • I followed that but I dont want to use the Session session provided in my new class – deepak mann Feb 09 '17 at 11:32
  • That comment doesn't provide nearly enough explanation of what you mean. Please read https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/ and put more effort into making your question clear. – Jon Skeet Feb 09 '17 at 11:34

1 Answers1

2

Static fields and methods are linked to the class. They can be invoked just by the classname and dot operator.

Non static fields and members are linked to an instance of the class. They need an object of the class to be invoked. In the same class there is a special reference which refers to the currently executing object called this.

Class is a blueprint and its instances are the realization of that blueprint. When an object is created then in memory the space is allocated. We invoke non static methods on the object.

Your method getDocumentationList is non static meaning it requires an object of class1 so that it could be invoked on that object. You are calling it using a class name instead you need to create an object and then invoke the method.

Second option is to declare getDocumentationList as static.

nits.kk
  • 5,204
  • 4
  • 33
  • 55
  • I followed that but I dont want to use the Session session provided in my new class – deepak mann Feb 09 '17 at 11:34
  • I edited my answer, if you wish to invoke amethod without the object then declare it static – nits.kk Feb 09 '17 at 11:36
  • did it helped, my question and the others.. you can choose to upvote the answers or questions which helped u understand and also can choose to mark the answer as accepted. – nits.kk Feb 09 '17 at 14:35